Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to Extend an Array After Initialisation in Java?

  • Last Updated : 05 Aug, 2021

In java, the arrays are immutable i.e if the array is once assigned or instantiated the memory allocated for the array can’t be decreased or increased. But there is one form of a solution in which we can extend the array.

Extending an array after initialization: As we can’t modify the array size after the declaration of the array, we can only extend it by initializing a new array and copying the values of the old array to the new array, and then we can assign new values to the array according to the size of the array declared.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

Below are the examples to show extending the array after initialization.

Example 1:


// java program to demonstrate
// extending an array
import java.lang.*;
class ExtendingArray {
    public static void main(String[] args)
        // initializing string array
        String[] words = new String[] { "G", "E", "E" };
        // allocating space for 5 strings
        // in the extended array
        String[] extendWords = new String[5];
        // adding new string
        // at index 3 and 4
        extendWords[3] = "K";
        extendWords[4] = "S";
        // copying the array elements
        // to new extended array
        System.arraycopy(words, 0, extendWords, 0,
        // printing the extended array
        // elements
        for (String str : extendWords) {

Example 2: 


// Java program to demonstrate
// extending an array
import java.lang.*;
class ExtendingArray {
    public static void extendedArray()
        // initializing integers to array int
        int[] num = new int[] { 1, 2, 3, 4, 5, 6 };
        // allocating space for 10 integers
        int[] extendnum = new int[10];
        // adding new integers
        // at index 6,7,8,9
        extendnum[6] = 7;
        extendnum[7] = 8;
        extendnum[8] = 9;
        extendnum[9] = 10;
        // copying old array to new array
        System.arraycopy(num, 0, extendnum, 0, num.length);
        // print the elements of
        // extended array using for-each
        for (int str : extendnum)
    public static void main(String[] args)
        // create an instance
        ExtendingArray exarr = new ExtendingArray();
        // extend an array and print them


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!