How to copy array and increase size dynamically in Java ?

Below is a sample code snippet in Java demonstrating the steps to copy the array elements to a new array and increase the size dynamically. The program using the Arrays.copyOf method which lets the developers to create new array with a new size as well as copy the content of the old array to it at the same time.

How to copy array and increase size dynamically in Java ?

Code Snippet

package com.abundantcode;
import java.util.Arrays;
import java.util.List;
public class Main {

    public static void main(String[] args)
    {
        String[] input = {"Java", "Abundantcode.com", "MCA Lab Programs", "B.E Java Lab Programs", "BCA Java Programs"};

        System.out.println("Array before copying: "+input.length);

        String[] outPutArray = Arrays.copyOf(input, 10);

        System.out.println("After Copying : "+ outPutArray.length);
       

    }
}
image
%d