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 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.