How to copy array and increase size dynamically in Java ?

Published on July 3, 2015 by abundantcode

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 3357 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", "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

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

Leave a Comment


x

You might be Interested in these Posts

C Program to display Inverted half-pyramid using *

Problem Write a program in C to print Inverted half-pyramid using * as shown below.The program shou...

How to use array Rank in C# ?

In C# , the Rank property of the array is is used to get the rank of the array. In simple terms , ra...

How to get the Number of Elements in an MultiDimensional Array in C# ?

In one of the previous articles , we demonstrated the usage of the Length property of the array in C...