I have to find dynamic array using in java. I know I can use ArrayList or List but i cant use them now, I really made an effort about finding this, but i couldn't find how to use non static(dynamic) array without Collections, thanks
-
2Why do you need to create a dynamic array? Why can't you use collections? What are you trying to do? How are you failing?npinti– npinti2012-04-03 06:02:03 +00:00Commented Apr 3, 2012 at 6:02
-
It is my project and I cope with this about 6 days and still i cant find how to create dynamic array without collections, just collections are prohibitedengineer– engineer2012-04-03 06:07:28 +00:00Commented Apr 3, 2012 at 6:07
-
If you can't use collections, you will have to either create an array each time you add something, like suggested below, or else, create a very large array the first time and keep adding stuff to it. If I where you I would go with the first choice.npinti– npinti2012-04-03 06:09:15 +00:00Commented Apr 3, 2012 at 6:09
-
you just see the source code of arraylist and understandBalaswamy Vaddeman– Balaswamy Vaddeman2012-04-03 06:13:30 +00:00Commented Apr 3, 2012 at 6:13
7 Answers
Arrays have fixed length in Java. There's no way around that.
If you want to add an N:th element to an array, arr with length N-1 then you'll have to
- Create a new array.
- Copy over the content of the old array to the new array
- Replace all references to the old array with references to the new on.
In code these steps correspond to
int[] newArr = new int[N];
System.arraycopy(arr, 0, newArr, 0, arr.length);
arr = newArr;
or, slightly shorter by using Arrays.copyOf from Java 6:
int[] newArr = Arrays.copyOf(arr, N);
arr = newArr;
7 Comments
Normal arrays in Java are not dynamic, so when you want to change the size of the array, you need to create a new one and copy the content of the previous one into it.
You can do that using Arrays#copyOf method to create and copy it in a simple way:
int[] myNewIntArray = Arrays.copyOf(oldIntArray, newArraySize);
You can implement your own dynamic array. Create an array of size 1 (or n if an initial size is provided), then double the size and copy existing elements once it reaches the capacity.
And this is going to have an amortized cost of 3 per operation, 3n for n elements, meaning you can maintain that in o(n) time.
A very simple naive implementation (for int array) would be:
class DynamicIntArray
{
private int capacity;
private int[] array;
private int size = 0;
public DynamicIntArray()
{
this(1);
}
public DynamicIntArray(int capacity)
{
this.capacity = capacity;
array = new int[this.capacity];
}
public void add(int a)
{
if (size == capacity)
resize();
array[size] = a;
size++;
}
private void resize()
{
capacity *= 2;
array = Arrays.copyOf(array, capacity);
}
// Implement the rest
}
Here is how you would use it:
DynamicIntArray dynamicArray = new DynamicIntArray();
dynamicArray.add(1);
dynamicArray.add(2);
dynamicArray.add(3);
If you change it to use generics you can use it for anything, not just integers.
19 Comments
You just allocate them like this:
int[] array = new int[17];