How to Extend an Array After Initialisation in Java? Last Updated : 28 Oct, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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. In this article, we will learn to increase the size of 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.Below are the examples to show extending the array after initialization.Example 1: Java // Java program to demonstrate // extending an array import java.lang.*; class ExtendingArray { public static void main(String[] args) { String[] str = new String[] { "G", "E", "E" }; // allocating space for 5 strings // in the extended array String[] ext = new String[5]; ext[3] = "K"; ext[4] = "S"; // copying the array elements // to new extended array System.arraycopy(str, 0, ext, 0, str.length); for (String s : ext) { System.out.print(s); } } } OutputGEEKSExample 2: Java // Java program to demonstrate // extending an array import java.lang.*; class ExtendingArray { public static void extendedArray() { int[] arr = new int[] { 1, 2, 3, 4, 5, 6 }; // allocating space for 10 integers int[] arr2 = new int[10]; arr2[6] = 7; arr2[7] = 8; arr2[8] = 9; arr2[9] = 10; // copying old array to new array System.arraycopy(arr, 0, arr2, 0, arr.length); for (int str : arr2) System.out.print(str + " "); } public static void main(String[] args) { ExtendingArray arr = new ExtendingArray(); arr.extendedArray(); } } Output1 2 3 4 5 6 7 8 9 10 Comment More infoAdvertise with us Next Article Sort an Array and Insert an Element Inside Array in Java K kadiummanisha Follow Improve Article Tags : Java Java Programs Java-Arrays Java-Array-Programs Practice Tags : Java Similar Reads How to Fill (initialize at once) an Array in Java? An array is a group of like-typed variables that are referred to by a common name. In this, article we will learn about Filling array in Java while Initialization.Example:Java// Java program to fill the element in an array import java.util.*; public class Geeks { public static void main(String args[ 3 min read How to Add Element in Java ArrayList? Java ArrayList class uses a dynamic array for storing the elements. It is like an array, but there is no size limit. We can add or remove elements anytime. So, it is much more flexible than the traditional array. Element can be added in Java ArrayList using add() method of java.util.ArrayList class. 2 min read How to Add an Element at Particular Index in Java ArrayList? ArrayList.add() method is used to add an element at particular index in Java ArrayList. Syntax: public void add(int index, Object element) ; Parameters: index -position at which the element has to be inserted. The index is zero-based.element - the element to be inserted at the specified position. Ex 2 min read Sort an Array and Insert an Element Inside Array in Java Sorting an array can be done by using inbuilt sort function while for the insertion we have to create a new array to do so as arrays in Java are immutable. To learn more about sorting in Java follow the article mentioned below: Sorting: Arrays.sort() in Java with examples Approach 1: Create a new ar 3 min read How to Declare an ArrayList with Values in Java? ArrayList is simply known as a resizable array. Declaring an ArrayList with values is a basic task that involves the initialization of a list with specific elements. It is said to be dynamic as the size of it can be changed. Proceeding with the declaration of ArrayList, we have to be aware of the co 2 min read Java Program to Convert an Array into a List In Java, arrays and lists are two commonly used data structures. While arrays have a fixed size and are simple to use, lists are dynamic and provide more flexibility. There are times when you may need to convert an array into a list, for instance, when you want to perform operations like adding or r 4 min read Like