
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Extend Size of an Integer Array in Java
What is an array?
An array is a data structure that holds a collection of elements (values or variables) of the same size in memory, with each element being identified by one or more indices or keys.
How does Java arraycopy() transfer array elements?
The Java arraycopy() method copies elements from a source array to a destination array. It begins at a specified position in the source array and transfers a specified number of elements to the destination array, starting at a given position. The number of elements copied corresponds to the length parameter. Elements from positions srcPos to srcPos + length - 1 in the source array are copied to positions destPos to destPos + length - 1 in the destination array.
Syntax:
public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
Extend the size of an Integer array
Integer Array: An array in Java that holds a collection of elements of type Integer, a wrapper class for the primitive data type int.
The following are the steps to extendthe size of an Integer array ?
- Step 1: Initialize the original array: The code begins by initializing an array of Integer values and printing its elements. an array arr is created with initial elements. A loop prints the values of the array before any changes are made.
Integer[] arr = new Integer[] { 50, 100, 150, 200, 400, 500, 800, 1000};
System.out.println("All the elements before extending the array...");
for (Integer i : arr)
System.out.println(i);
-
Step 2: Create a new larger Array and add new elements: A new array new_size is created with more space, and some new values are assigned to specific positions in the array. The new array new_size has 15 elements, with the new values added starting from index 8.
Integer[] new_size = new Integer[15];
new_size[8] = 2000;
new_size[9] = 3000;
new_size[10] = 4000;
new_size[11] = 5000;
new_size[12] = 6000;
new_size[13] = 9000;
new_size[14] = 10000;
-
Step 3: Copy elements from the original array to the new array: The System.arraycopy method is used to copy the original array's elements into the new larger array. The arr array elements are copied into the new_size array starting from index 0.
System.arraycopy(arr, 0, new_size, 0, arr.length);
Java program to extend the size of an integer array
The following is an example of extending the size of an integer array ?
public class Demo { public static void main(String[] args) { Integer[] arr = new Integer[] { 50, 100, 150, 200, 400, 500, 800, 1000}; System.out.println("All the elements before extending the array..."); for (Integer i:arr) System.out.println(i); Integer[] new_size = new Integer[15]; new_size[8] = 2000; new_size[9] = 3000; new_size[10] = 4000; new_size[11] = 5000; new_size[12] = 6000; new_size[13] = 9000; new_size[14] = 10000; System.arraycopy(arr, 0, new_size, 0, arr.length); System.out.println("All the elements after extending the array..."); for (Integer i: new_size) System.out.println(i); } }
Output
All the elements before extending the array... 50 100 150 200 400 500 800 1000 All the elements after extending the array... 50 100 150 200 400 500 800 1000 2000 3000 4000 5000 6000 9000 10000