How to resize an array in Java
Overview
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.
The system.arraycopy() function
The arraycopy() function is used to copy elements from one array to another.
Syntax
void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
Parameters
-
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.
Return value
This function returns no value. Its return type is void.
Resize an array to a larger size
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:
Code example 1
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));}}
Explanation
- Line 5: We declare an integer array of three elements, named
oldArray, with values1,2, and3. - Line 6: We declare an integer array of six elements, named
newArray. It is double the size ofoldArray. - Line 7: We copy the elements of
oldArrayintonewArrayusing thearraycopy()method. - Line 8: We set the
oldArraytonull, freeing up the memory it was using. - Line 10: We print out the contents of
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.
Resize an array to a smaller size
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:
Code example 2
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));}}
Explanation
- Line 5: We declare an array
oldArrayof size10. - Line 6: We create a new array named
newArray, that is half the size ofoldArray. - Line 7: We copy the elements of
oldArraystarting from index5intonewArrayusing thearraycopy()method. We have copied only the last 5 elements fromoldArrayintonewArray. - Line 8: We set
oldArraytonull, freeing up the memory it was using. - Line 10: We print out the contents of
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.
The arrays.copyOf() function
The 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.
Syntax
public static int[] copyOf(int[] array,int length)
Parameters
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.
Example
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));}}
Explanation
- Line 4: We declare an integer array of five elements, named
oldArray, with values1,2,3,4, and5. - Line 5: We create a new array named
newArray. It is larger in size thanoldArray. - Line 6: We copy the elements of
oldArrayintonewArrayusing theArrays.copyOf()method. - Line 7: We set
oldArraytonull, freeing up the memory it was using. - Line 9: We print out the contents of
newArray.