![]() |
| ||
| adding to a dynamic array size... 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... A a1 = new A(1, 2, "test"); |
| ||
| Re: adding to a dynamic array size... Why not encompass your existing class around the ArrayList. Then just access the .add() method? |
| ||
| 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. |
| ||
| 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 |
| ||
| Re: adding to a dynamic array size... Quote:
|
| ||
| 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... :) |
| ||
| Re: adding to a dynamic array size... You could even use Vector class. Its implementation is much simpler. Its in java.util package |
| ||
| Re: adding to a dynamic array size... Quote:
if you make 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. int [] arr = new int[10]; or you can easily use arrayList as they told you, which already does that. |
| All times are GMT -4. The time now is 10:36 pm. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC