An array is a structure of fixed-size, which can hold items of the same data type. It can be an array of integers, an array of floating-point numbers, an array of strings or even an array of arrays (such as 2-dimensional arrays). Arrays are indexed, meaning that random access is possible.
Basic Operations performed in an Arrays
Access: Retrieving an element's value by index.
Insertion: Adding an element at a specific position, potentially shifting subsequent elements.
Deletion: Removing an element, potentially shifting subsequent elements.
Search: Finding the index of a specific element.
Sort: Arranging elements in a specific order.
Update: Modifying the value of an element.
Merge: Combining two arrays into one.
Split: Dividing an array based on criteria.
Concatenate: Joining multiple arrays into one
Traversal: Visiting each element once.
Applications
Data Storage: Storing collections of data.
Algorithms: Sorting, searching, and other computations.
Databases: Handling query results.
Graphics: Representing pixel data.
Mathematics: Manipulating numerical data.
Web Development: Handling form data and APIs.
Dynamic Programming: Solving problems efficiently.
User Interfaces: Managing lists and tables.
Network Programming: Organizing data packets.
AI and ML: Representing datasets.
public class Main {
public static void main(String[] args) {
// Creating an array
int[] numbers = {5, 2, 9, 1, 7};
// Accessing elements
int firstElement = numbers[0];
System.out.println("First element: " + firstElement);
// Insertion - adding 10 at index 2
numbers[2] = 10;
// Deletion - removing element at index 3
int deletedElement = numbers[3];
System.out.println("Deleted element: " + deletedElement);
for (int i = 3; i < numbers.length - 1; i++) {
numbers[i] = numbers[i + 1];
}
// Searching for element 7
int searchElement = 7;
int index = -1;
for (int i = 0; i < numbers.length; i++) {
if (numbers[i] == searchElement) {
index = i;
break;
}
}
System.out.println("Index of element 7: " + index);
// Sorting the array
Arrays.sort(numbers);
// Updating - changing element at index 1 to 8
numbers[1] = 8;
// Merging two arrays
int[] anotherArray = {3, 6, 4};
int[] mergedArray = new int[numbers.length + anotherArray.length];
System.arraycopy(numbers, 0, mergedArray, 0, numbers.length);
System.arraycopy(anotherArray, 0, mergedArray, numbers.length, anotherArray.length);
// Splitting the merged array into two
int splitIndex = 4;
int[] splitArray1 = Arrays.copyOfRange(mergedArray, 0, splitIndex);
int[] splitArray2 = Arrays.copyOfRange(mergedArray, splitIndex, mergedArray.length);
// Concatenating two arrays
int[] array1 = {1, 2, 3};
int[] array2 = {4, 5, 6};
int[] concatenatedArray = new int[array1.length + array2.length];
System.arraycopy(array1, 0, concatenatedArray, 0, array1.length);
System.arraycopy(array2, 0, concatenatedArray, array1.length, array2.length);
// Traversal - printing all elements of the array
System.out.println("Concatenated array: ");
for (int num : concatenatedArray) {
System.out.print(num + " ");
}
}
}