how to resize array when the program is running? because of i cannot
determine the size of the array before run the program .
thanks!
VisionSet - 21 Feb 2006 19:14 GMT
> how to resize array when the program is running? because of i cannot
> determine the size of the array before run the program .
>
> thanks!
You can't array lengths are fixed at the time they are instantiated.
Use an ArrayList which internally creates new Array objects and copies the
objects at your array indexes between them as your size requirements change.
So why reinvent the wheel?
--
Mike W
mike - 21 Feb 2006 19:43 GMT
If you have to use an array for some reason, you would just have to
check it and see if the amount of "spots" you have used fills the
array. Then call a method which returns a new array with the same info
only twice the size. But I agree with the above. An array list would
be much better.
Dimitri Maziuk - 22 Feb 2006 17:15 GMT
VisionSet sez:
>> how to resize array when the program is running? because of i cannot
>> determine the size of the array before run the program .
[quoted text clipped - 6 lines]
> objects at your array indexes between them as your size requirements change.
> So why reinvent the wheel?
One obvious answer is a resizable array of primitive types.
However, you don't need to reinvent the wheel: dig into src.zip
and see how ArrayList does resizing. Then re-implement the code.
(Straight copy-pasting may not sit well with Sun's license terms.
I'm not sure, though -- who reads the licenses anyway.)
Dima

Signature
Surely there is a polite way to say FOAD. -- Shmuel Metz
"Go forth and multiply". -- Paul Martin
Oliver Wong - 21 Feb 2006 19:18 GMT
> how to resize array when the program is running? because of i cannot
> determine the size of the array before run the program .
Use an ArrayList instead of an array.
http://java.sun.com/j2se/1.5.0/docs/api/java/util/ArrayList.html
If there's a special reason you must use array, post it, and we'll
address actually creating arrays of the desired sizes.
- Oliver
tom fredriksen - 22 Feb 2006 18:45 GMT
> how to resize array when the program is running? because of i cannot
> determine the size of the array before run the program .
Here is an example of how you can do it:
byte[] b;
int size = 32;
b = new byte[size];
for(int c=0; c<size; c++) {
b[c] = (byte) c;
}
System.out.println("Finished 1");
size *= 2;
b = new byte[size];
for(int c=0; c<size; c++) {
b[c] = (byte) c;
}
System.out.println("Finished 2");
But you should of course consider using ArrayList instead.
/tom
Roedy Green - 24 Feb 2006 14:09 GMT
>how to resize array when the program is running? because of i cannot
>determine the size of the array before run the program .
There are two techniques. When it gets full, pause, allocate a bigger
array and copy over, and replace the original reference.
The second is to use an ArrayList which does this for you
automatically with extra overhead of method calls for every access.
The overhead is quite astounding. I am surprised it is not more of an
issue. People use ArrayList even when they know in advance the size
simply because they are more familiar with that idiom.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.