Array Operations in Java
Arrays are a fundamental data structure in Java that allow storing multiple elements of the same
type in a contiguous memory location. The basic operations that can be performed on arrays
include insertion, accessing elements, updating elements, and deletion.
1. Insertion – Adding Elements to an Array
Definition:
Insertion refers to the process of adding elements into an array. Since arrays in Java have a fixed
size, direct insertion is only possible at initialization or by modifying an existing index. If
dynamic insertion is needed, an array must be resized (which is why ArrayList is often
preferred for dynamic arrays).
Methods of Insertion:
1. At Initialization:
2. int[] numbers = {10, 20, 30, 40, 50}; // Inserting values at
initialization
3. Using Index Assignment:
4. int[] numbers = new int[5];
5. numbers[0] = 10; // Inserting elements at specific indices
6. numbers[1] = 20;
7. Expanding an Array: (Not directly possible, but can be done using Arrays.copyOf())
8. int[] oldArray = {1, 2, 3};
9. int[] newArray = Arrays.copyOf(oldArray, 4);
10. newArray[3] = 4; // Adding a new element
2. Accessing Elements – Using Indexing to Retrieve Data
Definition:
Accessing an element means retrieving the value stored at a specific index in an array. Java
arrays use zero-based indexing, meaning the first element is at index 0.
Example:
int[] numbers = {5, 10, 15, 20, 25};
System.out.println("Element at index 2: " + numbers[2]); // Output: 15
Accessing Using a Loop:
To print all elements of an array:
for (int i = 0; i < numbers.length; i++) {
System.out.println("Element at index " + i + ": " + numbers[i]);
}
3. Updating Elements – Modifying Existing Values
Definition:
Updating an element means replacing an existing value at a given index with a new value.
Example:
int[] numbers = {5, 10, 15, 20, 25};
numbers[2] = 50; // Changing value at index 2
System.out.println("Updated element at index 2: " + numbers[2]); // Output: 50
4. Deletion – Removing Elements from an Array
Definition:
Since Java arrays have a fixed size, elements cannot be directly deleted. However, deletion can
be simulated by:
1. Shifting elements to the left to remove a specific index.
2. Creating a new array without the element to remove.
Method 1: Shift Elements Left
int[] numbers = {10, 20, 30, 40, 50};
int deleteIndex = 2; // Removing element at index 2 (value 30)
for (int i = deleteIndex; i < numbers.length - 1; i++) {
numbers[i] = numbers[i + 1]; // Shift left
}
numbers[numbers.length - 1] = 0; // Optional: Set last element to default
value
System.out.println(Arrays.toString(numbers)); // Output: [10, 20, 40, 50, 0]
Method 2: Creating a New Array
int[] numbers = {10, 20, 30, 40, 50};
int deleteIndex = 2;
int[] newArray = new int[numbers.length - 1];
for (int i = 0, j = 0; i < numbers.length; i++) {
if (i != deleteIndex) {
newArray[j++] = numbers[i];
}
}
System.out.println(Arrays.toString(newArray)); // Output: [10, 20, 40, 50]
Summary of Operations:
Operation Description Example Code
Insertion Adding an element to an array arr[0] = 10;
Retrieving an element using
Accessing System.out.println(arr[2]);
index
Modifying an element at a
Updating arr[1] = 99;
given index
Removing an element by
for (int i = index; i < arr.length - 1; i+
Deletion shifting or creating a new +) arr[i] = arr[i + 1];
array