
- Example - Home
- Example - Environment
- Example - Strings
- Example - Arrays
- Example - Date & Time
- Example - Methods
- Example - Files
- Example - Directories
- Example - Exceptions
- Example - Data Structure
- Example - Collections
- Example - Networking
- Example - Threading
- Example - Applets
- Example - Simple GUI
- Example - JDBC
- Example - Regular Exp
- Example - Apache PDF Box
- Example - Apache POI PPT
- Example - Apache POI Excel
- Example - Apache POI Word
- Example - OpenCV
- Example - Apache Tika
- Example - iText
- Java Useful Resources
- Java - Quick Guide
- Java - Useful Resources
How to Extend an Array after Initialisation in Java
Extending an array after initialization in Java involves creating a new array with a larger size and copying the elements of the original array into it. Java arrays have a fixed size, so to add more elements, a new array must be created to accommodate the additional data.
Extend an Array after Initialisation
The System.arraycopy() method in Java transfers elements from a given source array to a destination array. The copying starts at the specified index in the source array and continues to the designated position in the destination array. A sequence of elements is copied from the source, and the number of elements copied corresponds to the provided length.
Elements located between srcPos and srcPos + length - 1 in the source array are moved to positions between destPos and destPos + length - 1 in the destination array.
Syntax
The following is the syntax for System.arraycopy() method
public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
Example 1
A new array called extended is created with a larger size than the original array names. Elements "D" and "E" are added at indices 3 and 4 of the new array, and the System.arraycopy() method is used to copy all elements from the original array into the new one. The following example shows how to extend an array after initialization by creating an new array
public class Main { public static void main(String[] args) { String[] names = new String[] { "A", "B", "C" }; String[] extended = new String[5]; extended[3] = "D"; extended[4] = "E"; System.arraycopy(names, 0, extended, 0, names.length); for (String str : extended){ System.out.println(str); } } }
Output
A B C D E
Example 2
This example follows a similar process as the first one. A new array, extended, is created with additional size to accommodate new elements "Prasad" and "Mammahe". The original array names is then copied to this new extended array using System.arraycopy(). The result shows both the original and newly added elements.The following is the code of arrays expansion
public class Main { public void extendArraySize() { String[] names = new String[] {"Sai", "Ram", "Krishna"}; String[] extended = new String[5]; extended[3] = "Prasad"; extended[4] = "Mammahe"; System.arraycopy(names, 0, extended, 0, names.length); for (String str : extended) System.out.println(str); } public static void main(String[] args) { new Main().extendArraySize(); } }
Output
Sai Ram Krishna Prasad Mammahe .