Example of a One Dimensional Array
Below is an example of One Dimensional Array:
// 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.