943,568 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 43187
  • Java RSS
May 6th, 2007
0

adding to a dynamic array size...

Expand Post »
I can't figure how to get this going, for every object I create I want to add that to the existing array. I read about arraycopy but I don't know how to increase the existing size of the array and always end up with array out of index error...

java Syntax (Toggle Plain Text)
  1. A a1 = new A(1, 2, "test");
  2.  
  3. size = 1;
  4. sizeCounter = 1;
  5. A a[] = new A[size];
  6. a[0] = a1;
  7.  
  8. A a2 = new A(3, 4, "test2");
  9.  
  10. sizeCounter++;
  11. if (sizeCounter>=size){
  12. size++;}
  13.  
  14. A[size-1] = a2;
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
rugae is offline Offline
27 posts
since Mar 2007
May 6th, 2007
0

Re: adding to a dynamic array size...

Why not encompass your existing class around the ArrayList. Then just access the .add() method?
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
May 7th, 2007
0

Re: adding to a dynamic array size...

iamthwee is right, you should be using ArrayList. You cannot increase the size of an existing array. You have to create a new array that is one larger than the current array, then you can use System.arraycopy to copy the existing elements from the old to the new array, than add the new element.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
May 7th, 2007
0

Re: adding to a dynamic array size...

Doh! Ok, then, I'll just increase the size of the array. I was doing somethig similar to below, everything is done but I thought it would be neat idea to increase the array dynamically.

prompt user for input
checkinput()
if no dupe add to array
else error msg
Last edited by rugae; May 7th, 2007 at 7:44 am.
Reputation Points: 10
Solved Threads: 0
Light Poster
rugae is offline Offline
27 posts
since Mar 2007
May 7th, 2007
0

Re: adding to a dynamic array size...

Click to Expand / Collapse  Quote originally posted by rugae ...
Doh! Ok, then, I'll just increase the size of the array. I was doing somethig similar to below, everything is done but I thought it would be neat idea to increase the array dynamically.

prompt user for input
checkinput()
if no dupe add to array
else error msg
You obviously missed my post then...
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Aug 14th, 2009
0

Re: adding to a dynamic array size...

Guys you can use

import java.util.List;

List <String> wordset = new ArrayList <String>();

usimg this you can get dynamic arrays working well...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
pasanindeewara is offline Offline
3 posts
since Jul 2009
Aug 14th, 2009
0

Re: adding to a dynamic array size...

You could even use Vector class.
Its implementation is much simpler.
Its in java.util package
Reputation Points: 27
Solved Threads: 8
Light Poster
harsh2327 is offline Offline
44 posts
since May 2008
Aug 15th, 2009
0

Re: adding to a dynamic array size...

Click to Expand / Collapse  Quote originally posted by rugae ...
Doh! Ok, then, I'll just increase the size of the array. I was doing somethig similar to below, everything is done but I thought it would be neat idea to increase the array dynamically.

prompt user for input
checkinput()
if no dupe add to array
else error msg
you need to know that the array that you use in java is not flexible to increase the size of the array by itself, the array is a STATIC data structure.

if you make
Java Syntax (Toggle Plain Text)
  1. int [] arr = new int[10];
this array will remain in your scope of length 10, unless you make a new array that is nor related to that one and of bigger size and then put the elements of the first array in the second array and then make the first array point to the second array.
Java Syntax (Toggle Plain Text)
  1. int [] arr = new int[10];
  2. /* put data in the 10 elements of arr
  3. -----
  4. ----
  5. */
  6. int []arr2 = new int[arr.length * 2];
  7. //copy the elements of arr to the first 10 elements of arr2
  8. arr = arr2;

or you can easily use arrayList as they told you, which already does that.
Last edited by eng.M4AH; Aug 15th, 2009 at 12:14 pm.
Reputation Points: 17
Solved Threads: 1
Newbie Poster
eng.M4AH is offline Offline
8 posts
since Dec 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Help my Circular Linked List
Next Thread in Java Forum Timeline: dynamic website





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC