The size of an array is the number of elements it can hold. In Java, the array size is fixed when it is created.
The easiest way to resize an array is to create a new array with the desired size and copy the contents of the old array into it. We can delete the old array by setting it to null.
For this, we use the arraycopy()
method of the System
class or copyOf()
method of the java.util.Arrays
class.
system.arraycopy()
functionThe arraycopy()
function is used to copy elements from one array to another.
void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
src
: This is the source array.srcPos
: This is the starting position in the source array.dest
: This is the destination array.destPos
: This is the starting position in the destination array.length
: This is the number of elements to be copied.This function returns no value. Its return type is void
.
Below is the code example of resizing an array to a larger size. For example, if we have an array of size 3
and we want to resize it to size 6
, we can do it as follows:
import java.util.Arrays;class HelloWorld {public static void main( String args[] ) {int[] oldArray = {1, 2, 3};int[] newArray = new int[6];System.arraycopy(oldArray, 0, newArray, 0, 3);oldArray = null;System.out.println(Arrays.toString(newArray));}}
oldArray
, with values 1
, 2
, and 3
.newArray
. It is double the size of oldArray
. oldArray
into newArray
using the arraycopy()
method. oldArray
to null
, freeing up the memory it was using.newArray
.Note: This will only work if the new array is larger than the old one. If we want to resize an array to a smaller size, we'll have to create a new array and copy only the desired elements into it.
The code example below shows how to resize an array to a smaller size. For example, consider an array of size 10
and we want to resize it to size 5
by copying the last 5 elements:
import java.util.Arrays;class HelloWorld {public static void main( String args[] ) {int[] oldArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};int[] newArray = new int[5];System.arraycopy(oldArray, 5, newArray, 0, 5);oldArray = null;System.out.println(Arrays.toString(newArray));}}
oldArray
of size 10
.newArray
, that is half the size of oldArray
. oldArray
starting from index 5
into newArray
using the arraycopy()
method. We have copied only the last 5 elements from oldArray
into newArray
.oldArray
to null
, freeing up the memory it was using. newArray
.The java.util.Arrays
class has a method named copyOf()
, which can also be used to increase or decrease the size of an array. We can also use this method to resize an Array.
arrays.copyOf()
functionThe copyOf()
method is a utility function that creates a new array with the same contents as an existing array. The new array is always of the same type as the original array. This method is useful for creating a copy of an array that will not be modified.
public static int[] copyOf(int[] array,int length)
array
: This is the array that we want to resize or copy.length
: This is the size of the new array. If the new size is greater than the size of the original array, extra elements will be filled with the default values. If the new size is less than the old size, extra elements will be truncated. This function is also overloaded for other types. import java.util.Arrays;class ArrayResize {public static void main(String[] args) {int[] oldArray = {1,2,3,4,5};int newSize = 10; // resize to 10 elementsint[] newArray = Arrays.copyOf(oldArray, newSize);oldArray = null;//printing elements of new arraySystem.out.println(Arrays.toString(newArray));}}
oldArray
, with values 1
, 2
, 3
, 4
, and 5
.newArray
. It is larger in size than oldArray
. oldArray
into newArray
using the Arrays.copyOf()
method.oldArray
to null
, freeing up the memory it was using. newArray
.