๐ŸŽ‰ Special Offer !    Code: GET300OFF    Flat โ‚น300 OFF on every Java Course
Grab Deal ๐Ÿš€

Array Operations in DSA (Java)  


Introduction
  • In Data Structures, an array is a collection of elements of the same type stored at contiguous memory locations.
  • To work with arrays effectively, we perform different operations such as inserting, deleting, searching, updating, traversing, sorting, etc.
  • These operations help in solving real-life programming problems and are important from an interview and academic point of view.
  • Types of Array Operations:
    1. Traversal - Accessing each element of the array one by one.
    2. Insertion - Adding a new element at a specific index.
    3. Deletion - Removing an element from a specific index.
    4. Updating - Modifying an element at a particular index.
    5. Searching - Finding the index of a given element.
    6. Sorting - Arranging elements in ascending or descending order.

Array Operations Programs:-
  • 1. Traversal Operation:
    • In traversal operation, we can access or print all the array elements one by one.
    • We can use for loop and for-each loop to traverse an array, but the recommended way is using for-each loop.
    • Program:
      public class MainApp1
      {
          public static void main(String[] args)
          {
              int[] arr = {10, 20, 30, 40, 50};
      
              // Traversal using for-each loop
              for (int no : arr)
              {
                  System.out.print(no + " ");
              }
          }
      }
  • 2. Insertion Operation:
    • In insertion operation, we add a new element at a specific index of the array.
    • Since arrays are of fixed size in Java, inserting an element requires shifting the elements to the right from the given index.
    • Program:
      public class MainApp2
      {
          public static void main(String[] args)
          {
              int[] arr = {10, 20, 30, 40, 50};
              int pos = 2;     // index where we want to insert
              int element = 99;
      
              // Shifting elements to the right
              for (int i = arr.length - 1; i > pos; i--)
              {
                  arr[i] = arr[i - 1];
              }
              
              arr[pos] = element;   // Insert element at position
      
              // Traversing updated array
              for (int no : arr)
              {
                  System.out.print(no + " ");
              }
          }
      }
  • 3. Deletion Operation:
    • In deletion operation, we remove an element from a specific index of the array.
    • Since arrays are of fixed size in Java, we cannot reduce their size. Instead, we shift the elements to the left from the given index.
    • The last element is usually set to 0 (default value) or ignored after shifting.
    • Program:
      public class MainApp3
      {
          public static void main(String[] args)
          {
              int[] arr = {10, 20, 30, 40, 50};
      
              int pos = 2;   // index of element to delete
      
              // Shifting elements to the left
              for (int i = pos; i < arr.length - 1; i++)
              {
                  arr[i] = arr[i + 1];
              }
              
              arr[arr.length - 1] = 0;  // Optional: set last element to 0
      
              // Traversing updated array
              for (int no : arr)
              {
                  System.out.print(no + " ");
              }
          }
      }
  • 4. Updation Operation:
    • In updation operation, we modify the value of an existing element at a specific index of the array.
    • This operation is simple because arrays allow direct access to elements using their index.
    • Program:
      public class MainApp4
      {
          public static void main(String[] args)
          {
              int[] arr = {10, 20, 30, 40, 50};
      
              int pos = 2;     // index to update
              int newValue = 99;
      
              // Updating element at given index
              arr[pos] = newValue;
      
              // Traversing updated array
              for (int no : arr)
              {
                  System.out.print(no + " ");
              }
          }
      }
  • 5. Searching Operation:
    • In searching operation, we try to find the index of a given element in the array.
    • The simplest method is linear search, where we compare each element one by one until the element is found.
    • Program:
      public class MainApp5
      {
          public static void main(String[] args)
          {
              int[] arr = {10, 20, 30, 40, 50};
              
              int key = 30;
              boolean found = false;
      
              // Linear search
              for (int i = 0; i < arr.length; i++)
              {
                  if (arr[i] == key)
                  {
                      System.out.println("Element found at index: " + i);
                      found = true;
                      break;
                  }
              }
      
              if (!found)
              {
                  System.out.println("Element not found");
              }
          }
      }
  • 6. Sorting Operation:
    • In sorting operation, we arrange the array elements in a specific order, usually ascending or descending.
    • We can use the built-in Arrays.sort() method for simple ascending order sorting, or implement a custom algorithm like Bubble Sort for learning purposes.
    • Program 1: Using Arrays.sort()
      import java.util.Arrays;
      
      public class MainApp6a
      {
          public static void main(String[] args)
          {
              int[] arr = {40, 10, 30, 50, 20};
      
              // Sorting array in ascending order using Arrays.sort()
              Arrays.sort(arr);
      
              // Traversing sorted array
              for (int no : arr)
              {
                  System.out.print(no + " ");
              }
          }
      }
    • Program 2: Custom Sorting (Using Bubble Sort)
      public class MainApp6b
      {
          public static void main(String[] args)
          {
              int[] arr = {40, 10, 30, 50, 20};
      
              // Bubble Sort in ascending order
              for (int i = 0; i < arr.length - 1; i++)
              {
                  for (int j = 0; j < arr.length - i - 1; j++)
                  {
                      if (arr[j] > arr[j + 1])
                      {
                          // Swap arr[j] and arr[j+1]
                          int temp = arr[j];
                          arr[j] = arr[j + 1];
                          arr[j + 1] = temp;
                      }
                  }
              }
      
              // Traversing sorted array
              for (int no : arr)
              {
                  System.out.print(no + " ");
              }
          }
      }