Example of a One Dimensional Array

Below is an example of One Dimensional Array:

Java
// Java Program to implement
// One-Dimensional Array

// Driver Class
public class ArrayExample {
      // Main Function
    public static void main(String[] args)
    {
        // Declare and initialize an array of integers
        int[] numbers = { 10, 20, 30, 40, 50 };

        // Print the elements of the array
        System.out.println("Original Array:");
        printArray(numbers);

        // Accessing elements of the array
        System.out.println("\nElement at index 2: " + numbers[2]); 
          // Output: 30

        // Modifying an element of the array
        numbers[3] = 45;

        // Print the modified array
        System.out.println("\nModified Array:");
        printArray(numbers);

        // Calculating the sum of elements in the array
        int sum = calculateSum(numbers);
        System.out.println("\nSum of elements in the array: " + sum);
          // Output: 145
    }

    // Method to print the elements of an array
    public static void printArray(int[] arr)
    {
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
    }

    // Method to calculate the sum of elements in an array
    public static int calculateSum(int[] arr)
    {
        int sum = 0;
        for (int num : arr) {
            sum += num;
        }
        return sum;
    }
}

Output
Original Array:
10 20 30 40 50 

Element at index 2: 30

Modified Array:
10 20 30 45 50 

Sum of elements in the array: 155

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