1. Principle of expansion
(1) The size of Java array objects is fixed, and array objects cannot be expanded.
(2) Array expansion can be achieved flexibly by using the array copy method.
(3) System.arraycopy() can copy an array.
(4) Arrays.copyOf() can easily create a copy of an array.
(5) When creating a copy of the array and increasing the length of the array, the expansion of the array can be realized in a flexible way.
2. Create the array first
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
3. Method 1: Loop through the original array arry, the length is fixed, so use a for loop.
1 2 3 4 5 6 7 8 9 10 11 |
|
4. Method 2: Use the copyOf (original array name, new array length) method of the Arrays class of the java util package to copy.
1 2 3 4 5 6 |
|
The above is the detailed content of How to expand array in java. For more information, please follow other related articles on the PHP Chinese website!