| | |
adding to a dynamic array size...
![]() |
•
•
Join Date: Mar 2007
Posts: 27
Reputation:
Solved Threads: 0
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)
A a1 = new A(1, 2, "test"); size = 1; sizeCounter = 1; A a[] = new A[size]; a[0] = a1; A a2 = new A(3, 4, "test2"); sizeCounter++; if (sizeCounter>=size){ size++;} A[size-1] = a2;
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.
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
•
•
Join Date: Mar 2007
Posts: 27
Reputation:
Solved Threads: 0
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
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.
•
•
Join Date: Dec 2008
Posts: 8
Reputation:
Solved Threads: 1
•
•
•
•
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
if you make
Java Syntax (Toggle Plain Text)
int [] arr = new int[10];
Java Syntax (Toggle Plain Text)
int [] arr = new int[10]; /* put data in the 10 elements of arr ----- ---- */ int []arr2 = new int[arr.length * 2]; //copy the elements of arr to the first 10 elements of arr2 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.
![]() |
Similar Threads
- get length of a dynamic array (C++)
- works for static need help to make it dynamic (C++)
- dynamic array of structures problem (C++)
Other Threads in the Java Forum
- Previous Thread: Help my Circular Linked List
- Next Thread: dynamic website
| Thread Tools | Search this Thread |
-xlint actionlistener add android applet application array automation bank bi binary blackberry block bluetooth character class client code compile compiler component consumer database desktop developmenthelp eclipse equation error event fractal ftp functiontesting game gameprogramming givemetehcodez graphics gui health html hyper idea image infinite int j2me j2seprojects java javac javaee javame javaprojects jetbrains jni jpanel jtable julia learningresources lego linked linux mac main method mobile myregfun netbeans notdisplaying number online pearl printf problem program qt researchinmotion rotatetext rsa scanner screen server set singleton sms sort spamblocker sql string swing system textfields thread threads time title tree tutorial-sample update variablebinding windows xor






