How to copy array and increase size dynamically in Java ?

Published on July 3, 2015 by Senthil Kumar

Filed under Java

Last modified July 3, 2015

Print this page

rate 1 star rate 2 star rate 3 star rate 4 star rate 5 star
Your rating: none, Average: 0 (0 votes)

This article have been viewed 8844 times

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", "", "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);



Leave a Comment


You might be Interested in these Posts

How to Create an Empty array without defining the size in C# ?

When we create an array in C# , we tend to define the size of the array as shown below. String[] st...

Video - Angular Router - Brian Ford

Video - Angular Router - Brian Ford ...

Video - An Angular2 Todo App: First look at App Development in Angular2

In this screencast , David East , one of the core contributor of the Angular provides a glimpse of A...