How to Resize an Array While Keeping the Current Elements in Java
- Resize an Array in Java
-
Resize an Array by Using the
arraycopy()
Method in Java -
Resize an Array by Using the
copyOf()
Method in Java -
Resize an Array by Using a
for
Loop in Java
This tutorial introduces how you can resize an array while keeping all of its current elements in Java. We included some example programs for you to refer to when executing a program in this field.
An array is defined as a container used to store similar types of elements in Java. It is a fixed-size container, which means if an array is of a 10 size, it can only store 10 elements - this is one of the limitations of an array.
In this article, we will learn to resize an array by using some built-in methods such as the arraycopy()
and copyOf()
functions and some custom codes.
Resize an Array in Java
The topmost alternate of the dynamic array is the ArrayList
class of collection framework that can store any number of elements, and grows dynamically. The first thing we can do is to create an ArrayList
and copy all the array elements into it. At last, we have a new size of array. See the example below:
import java.util.ArrayList;
import java.util.List;
public class SimpleTesting {
public static void main(String[] args) {
int arr[] = new int[] {12, 34, 21, 33, 22, 55};
for (int a : arr) {
System.out.print(a + " ");
}
List<Integer> list = new ArrayList<>();
for (int a : arr) {
list.add(a);
}
System.out.println("\n" + list);
list.add(100);
System.out.println(list);
}
}
Output:
12 34 21 33 22 55
[12, 34, 21, 33, 22, 55]
[12, 34, 21, 33, 22, 55, 100]
Resize an Array by Using the arraycopy()
Method in Java
Java provides a method arraycopy()
that belongs to the System
class and can be used to create a copy of an array. In this example, we’re creating a new array of a larger size and then copying all the original array elements into it by using the arraycopy()
method. Follow the example program below:
public class SimpleTesting {
public static void main(String[] args) {
int arr[] = new int[] {12, 34, 21, 33, 22, 55};
for (int a : arr) {
System.out.print(a + " ");
}
int arr2[] = new int[10];
System.arraycopy(arr, 0, arr2, 0, arr.length);
System.out.println();
for (int a : arr2) {
System.out.print(a + " ");
}
System.out.println();
arr2[6] = 100;
for (int a : arr2) {
System.out.print(a + " ");
}
}
}
Output:
12 34 21 33 22 55
12 34 21 33 22 55 0 0 0 0
12 34 21 33 22 55 100 0 0 0
Resize an Array by Using the copyOf()
Method in Java
The Java Arrays
class provides a method copyOf()
, which can be used to create a new size array by copying all the original array elements. This process takes two arguments: the first is the original array, and the second is the size of the new array. See the example below:
import java.util.Arrays;
public class SimpleTesting {
public static void main(String[] args) {
int arr[] = new int[] {12, 34, 21, 33, 22, 55};
for (int a : arr) {
System.out.print(a + " ");
}
int arr2[] = Arrays.copyOf(arr, 10);
System.out.println();
for (int a : arr2) {
System.out.print(a + " ");
}
System.out.println();
arr2[6] = 100;
for (int a : arr2) {
System.out.print(a + " ");
}
}
}
Output:
12 34 21 33 22 55
12 34 21 33 22 55 0 0 0 0
12 34 21 33 22 55 100 0 0 0
Resize an Array by Using a for
Loop in Java
This method is straightforward and is an older approach where we use a for
loop and assign original array elements into the newly-created array in each iteration. We just create a new array with a bigger size and copy all the elements into it by using the loop. See the example below:
public class SimpleTesting {
public static void main(String[] args) {
int arr[] = new int[] {12, 34, 21, 33, 22, 55};
for (int a : arr) {
System.out.print(a + " ");
}
int arr2[] = new int[10];
for (int i = 0; i < arr.length; i++) {
arr2[i] = arr[i];
}
System.out.println();
for (int a : arr2) {
System.out.print(a + " ");
}
}
}
Output:
12 34 21 33 22 55
12 34 21 33 22 55 0 0 0 0