Array Manipulations in Java

 


Array Manipulations in Java


  •  Creating Arrays in Java

In Java, arrays are a fundamental data structure and must be declared with a fixed size. The type of elements and the array size must be specified.




import java.util.Arrays;  

public class Main {
    public static void main(String[] args) {
        int[] arr = {10, 20, 30, 40, 50};

        System.out.println("The array: " + Arrays.toString(arr));
    }
}
          
       Output :   The  array: [10, 20, 30, 40, 50]


  • Inserting elements
 
In Java, arrays have a fixed size, so you cannot directly insert elements into an array without creating a new array. To handle this, you typically use an 'ArrayList' , which is a resizable array implementation.


   1. Inserting elements using a Fixed-Size Array:


If you need to insert elements into a fixed-size array, you would have to create a new array and copy elements over, which is less efficient.


import java.util.Arrays;

public class insertion {
    public static void main(String[] args) {
       
        int[] arr = {10, 20, 30, 40, 50};

        // New array with an additional element
        int[] newArr = new int[arr.length + 1];

        // Insert an element at index 2
        int insertIndex = 2;
        int newValue = 25;

        // Copy elements before the insertion point
        for (int i = 0; i < insertIndex; i++) {
            newArr[i] = arr[i];
        }

        // Insert the new element
        newArr[insertIndex] = newValue;

        // Copy elements after the insertion point
        for (int i = insertIndex; i < arr.length; i++) {
            newArr[i + 1] = arr[i];
        }

        // Print the new array
        System.out.println("Modified array: " + Arrays.toString(newArr));
    }
}

         
       Output :

          Modified array: [10, 20, 25, 30, 40, 50]



 2. Inserting elements using an ArrayList 


An 'ArrayList ' allows for dynamic resizing and provides a more straightforward way to insert elements.



public class insertion {
    public static void main(String[] args) {
        // Creating an ArrayList
        ArrayList<Integer> arrList = new ArrayList<>();

        // Adding elements to the ArrayList
        arrList.add(10);
        arrList.add(20);
        arrList.add(30);
        arrList.add(40);
        arrList.add(50);

        // Inserting an element at a specific position
        arrList.add(2, 25);  // Insert 25 at index 2

        // Printing the modified ArrayList
        System.out.println("Modified ArrayList: " + arrList);
    }
}
 
                 
          Output :

                 Modified ArrayList: [10, 20, 25, 30, 40, 50]



  • Accessing elements

  1. Accessing and printing the first element



public class access {
    public static void main(String[] args) {
       
        int[] arr = {10, 20, 30, 40, 50};

        System.out.println("First element: " + arr[0]);  

       
    }
}

               
                 Output :                     
                       first element : 10


  2.  Traversing the array  


public class access {
    public static void main(String[] args) {
       
        int[] arr = {10, 20, 30, 40, 50};

        System.out.println("First element: " + arr[0]);  

        // Traversing and printing all elements
        System.out.println("All elements:");
        for (int element : arr) {
            System.out.println(element);
        }

       
    }
}

              Output :

                        All elements:

                                       10

                                       20

                                       30

                                       40

                                       50


  3. Accessing elements using a for loop with indices


public class access {
    public static void main(String[] args) {
       
        int[] arr = {10, 20, 30, 40, 50};

        System.out.println("Accessing elements using indices:");
        for (int i = 0; i < arr.length; i++) {
            System.out.println("Element at index " + i + ": " + arr[i]);

       
    }
}

}


       Output : 
                 Accessing elements using indices:
                 Element at index 0: 10
                 Element at index 1: 20
                 Element at index 2: 30
                 Element at index 3: 40
                 Element at index 4: 50


  • Deleting elements 

1. Deleting elements using a new array


import java.util.Arrays;

public class deletion {
    public static void main(String[] args) {
        // Creating a one-dimensional array
        int[] arr = {10, 20, 30, 40, 50};

        // Deleting an element (create a new array)
        int[] newArr = new int[arr.length - 1];
        int deleteIndex = 2;  // Index of the element to be deleted (30)

        // Copy elements before the deletion point
        for (int i = 0; i < deleteIndex; i++) {
            newArr[i] = arr[i];
        }

        // Copy elements after the deletion point
        for (int i = deleteIndex; i < newArr.length; i++) {
            newArr[i] = arr[i + 1];
        }

        // Printing the modified array
        System.out.println("Modified array: " + Arrays.toString(newArr));
    }
}

        Output :

              Modified array: [10, 20, 40, 50]



    2. Deleting elements Using ' ArrayList ' :




import java.util.ArrayList;

public class delet {
    public static void main(String[] args) {
        // Creating an ArrayList
        ArrayList<Integer> arrList = new ArrayList<>();

        // Adding elements to the ArrayList
        arrList.add(10);
        arrList.add(20);
        arrList.add(30);
        arrList.add(40);
        arrList.add(50);

        // Deleting an element by index
        arrList.remove(2);  // Removes the element at index 2 (30)

        // Deleting an element by value
        arrList.remove(Integer.valueOf(40));  // Removes the first occurrence of 40

        // Printing the modified ArrayList
        System.out.println("Modified ArrayList: " + arrList);
    }
}

    
  Output :

           Modified ArrayList:  [10, 20, 50]






Comments

Popular posts from this blog

Object-Oriented Programming

The Strategic Importance of Agile Methodology in Contemporary Software Development