| | |
adding to a dynamic array size...
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
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 |
addball addressbook android api append applet application apps array arrays automation binary bluetooth businessintelligence button card chat class classes client code collision component crashcourse css csv database eclipse ee error exception fractal free game gis givemetehcodez graphics gui html ide image input integer integration j2me java javaarraylist javadoc javafx javaprojects jni jpanel julia jvm linux list loan loop machine map method methods migrate mobile netbeans newbie output phone physics print problem program programming project radio recursion reporting robot scanner screen se server service set size sms socket software sort sql string swing textfield threads transfer tree trolltech ubuntu utility windows






