Now I know arrays are fixed and their length cannot be changed. But I have to somehow use arrays in such a way that when the first array gets filled, I create a new array of double length and copy the values from the old array into the new array. I have to have this working an infinite amount of times. Also I cannot use array lists or anything under the collections interface.
-
1Look up `ArrayList`. Thats your solution. – Quijx Feb 28 '15 at 21:20
-
@Quijx "Also I cannot use array lists or anything under the collections interface." – Yoland Gao Feb 28 '15 at 21:20
-
Then duplicate the code of ArrayList. It's open-source. Why in hell wouldn't you use the standard, ready-made class that does exactly what you want? – JB Nizet Feb 28 '15 at 21:20
-
Like you don't know :p – JuniorCompressor Feb 28 '15 at 21:22
-
Another option is to use arraylists and convert back to array at the end – Yoland Gao Feb 28 '15 at 21:22
-
well, as you wont look at ArrayList then have you looked at System.arraycopy ? (like copy the source array, to a destination one+1 each time) – MrSimpleMind Feb 28 '15 at 21:22
-
@JBNizet believe me, I would much rather use an arraylist but they are not permitted on this assignment – Andrew Murchison Feb 28 '15 at 21:24
-
3You pretty much gave pseudo code of your own solution in your answer: create new array double the size, copy old array to it. So where exactly is the problem? – RealSkeptic Feb 28 '15 at 21:26
-
So it's an assignment, and it asks you to do what ArrayList is doing. Try to do it yourself, it's not that hard, and if you're stuck, look at the source code of ArrayList. – JB Nizet Feb 28 '15 at 21:26
-
why the down vote bellow? @RealSkeptic, I think I just wrote there what you said, isn't? or did I said something wrong? – Victor Feb 28 '15 at 21:28
-
@Victor - downvote not mine, but I think it may be because (a) you are supposed to be more specific in an answer, not just give vague guidelines (as opposed to a comment), (b) your formatting is bad, and (c) It's not clear what the name of the field is. – RealSkeptic Feb 28 '15 at 21:33
-
@RealSkeptic, Thanks, I ask just for the opinion. – Victor Feb 28 '15 at 21:34
-
If you can't seem to find back your previously asked questions, click anywhere your username or your profile picture appears as a link, like in top bar. – BalusC Feb 28 '15 at 21:35
2 Answers
You can create your own array class, with a int[] as a field, lets say: numbers. Whenever you need you increase the size you can do:
int[] tmpArray = new int[size you need]
copy first to tmp
numbers = tmp;
with this the original array will be garbage collected and the field will have a new array with the capacity you need.
They copy, you can do with your favority method, from System class, Arrays or even a old fashion loop

- 3,520
- 3
- 38
- 58
As you adequately put it, arrays cannot be resized. You have to allocate a new array and copy the content of the old array.
For example, a class with a dynamically growing array storage might resize itself like this:
int[] arr = {1, 2, 3};
void ensureCapacity(int targetSize) {
if (arr.length < targetSize) {
arr = Arrays.copyOf(arr, targetSize);
}
}
If your version of Java doesn't have Arrays.copyOf
, you can do it the good old-fashioned way using System.arraycopy
:
void ensureCapacity(int targetSize) {
if (arr.length < targetSize) {
int[] old = arr;
arr = new int[targetSize];
System.arraycopy(old, 0, arr, 0, old.length);
}
}

- 120,954
- 29
- 226
- 236