Program on Implementation of One-Dimensional Array in Java

Here is the Java program that demonstrate the implementation of the one-dimensional array and it performs the basic operations like initializing array, accessing elements, inserting elements, deleting elements, searching for elements and sorting elements:

Java
// Java Program to implement
// One Dimensional Array
import java.util.Arrays;

// Driver Class
public class ArrayExample {
      // Main Function
    public static void main(String[] args)
    {
        // Initializing an array
        int[] numbers = new int[5];

        // Inserting elements into the array
        numbers[0] = 10;
        numbers[1] = 30;
        numbers[2] = 20;
        numbers[3] = 50;
        numbers[4] = 40;

        // Accessing elements in the array
        System.out.println("Element at index 0: " + numbers[0]);
        // Output: 10

        System.out.println("Element at index 3: " + numbers[3]);
        // Output: 50

        // Deleting an element from the array
        deleteElement(numbers,2);
          // Delete element at index 2

        // Printing the array after deletion
        System.out.println(
            "Array after deleting element at index 2: "
            + Arrays.toString(numbers));

        // Searching for an element in the array
        int searchElement = 30;
        int index = searchElement(numbers, searchElement);
        if (index != -1) {
            System.out.println("Element " + searchElement
                               + " found at index "
                               + index);
        }
        else {
            System.out.println("Element " + searchElement
                               + " not found in the array");
        }

        // Sorting the array
        Arrays.sort(numbers);

        // Printing the sorted array
        System.out.println("Sorted array: "
                           + Arrays.toString(numbers));
    }

    // Function to delete an element from the array
    public static void deleteElement(int[] arr, int index)
    {
        if (index < 0 || index >= arr.length) {
            System.out.println(
                "Invalid index. Element cannot be deleted.");
        }
        else {
            for (int i = index; i < arr.length - 1; i++) {
                arr[i] = arr[i + 1];
            }
          
            arr[arr.length - 1] = 0; 
              // Set the last element to 0 or default
               // value
        }
    }

    // Function to search for an element in the array
    public static int searchElement(int[] arr, int element)
    {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == element) {
                  // Element found, return its index
                return i; 
            }
        }
      
          // Element not found
        return -1; 
    }
}

Output
Element at index 0: 10
Element at index 3: 50
Array after deleting element at index 2: [10, 30, 50, 40, 0]
Element 30 found at index 1
Sorted array: [0, 10, 30, 40, 50]

Application of One-Dimensional Array

One-Dimensional arrays are find the applications in the various domains because of its simplicity, efficiency and versatility. Here are the some common applications. They are:

  • Lists and Collections
  • Data Storage and Retrieval
  • Stacks and Queues
  • Matrices and Vectors
  • Dynamic Programming
  • Sorting and Searching Algorithms
  • Graph Algorithms
  • Histograms and Frequency Counting
  • Image Processing
  • Cryptography


One Dimensional Array in Java

An array is a type of Data Structure that can store collections of elements. These elements are stored in contiguous memory locations and the it provides efficient access to each element based on the index of the array element.

In this article, we will learn about a one-dimensional array in Java.

Similar Reads

What is an Array?

Arrays are commonly used for storing data and manipulating data in programming languages because they offer fast access to the elements based on their indices and provide efficient memory usage....

Example of a One Dimensional Array

Below is an example of One Dimensional Array:...

Organization of a One-Dimensional Array:

In memory, One-dimensional array in Java is the contiguous block of the memory locations allocated to the hold elements of the same data type. Every element occupied a fixed amount fixed amount of the memory, it is determined by data type of array. The elements in the array are stored sequentially in the memory by the one by one....

Basic Operations on One-Dimensional Array:

Basic operations on a one-dimensional array is include the accessing elements, inserting elements, deleting elements, searching for elements and sorting elements. Now we are discussing about every operation with the time complexity and space complexity:...

Program on Implementation of One-Dimensional Array in Java

Here is the Java program that demonstrate the implementation of the one-dimensional array and it performs the basic operations like initializing array, accessing elements, inserting elements, deleting elements, searching for elements and sorting elements:...