How increase dynamic array to save all it's elements?
Printable View
How increase dynamic array to save all it's elements?
If its a dynamic array, then it increases automatically, hence the name 'Dynamic' array.
quad64bit, dynamic array, for example such:
Code:int a[];
Is not a dynamic array. It is just an array. There is no way to expand an array after the fact, you must either copy the contents to a bigger array or pick a different data structure such as ArrayList.Code:int a[];
Code:import java.util.Arrays;
public class Test {
public static void main(String[] args) {
int[] values = { 5, 9, 3, 6 };
System.out.println(Arrays.toString(values));
// Add 7 to the array.
int[] temp = new int[values.length+1];
System.arraycopy(values, 0, temp, 0, values.length);
temp[values.length] = 7;
values = temp;
System.out.println(Arrays.toString(values));
}
}