Arrays

Arrays

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

  1. Access: Retrieving an element's value by index.

  2. Insertion: Adding an element at a specific position, potentially shifting subsequent elements.

  3. Deletion: Removing an element, potentially shifting subsequent elements.

  4. Search: Finding the index of a specific element.

  5. Sort: Arranging elements in a specific order.

  6. Update: Modifying the value of an element.

  7. Merge: Combining two arrays into one.

  8. Split: Dividing an array based on criteria.

  9. Concatenate: Joining multiple arrays into one

  10. Traversal: Visiting each element once.

Applications

  1. Data Storage: Storing collections of data.

  2. Algorithms: Sorting, searching, and other computations.

  3. Databases: Handling query results.

  4. Graphics: Representing pixel data.

  5. Mathematics: Manipulating numerical data.

  6. Web Development: Handling form data and APIs.

  7. Dynamic Programming: Solving problems efficiently.

  8. User Interfaces: Managing lists and tables.

  9. Network Programming: Organizing data packets.

  10. 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 + " ");
        }
    }
}

Did you find this article valuable?

Support Thirumalai by becoming a sponsor. Any amount is appreciated!