Example of single dimensional java array
Here we learn how to implement Example of single dimensional java array.
Arrays are an essential data structure in Java that allow storing multiple values of the same type in a contiguous memory location. In Java, an array is an object that holds elements of a similar data type and is accessed using an index.
The following example demonstrates how to declare, instantiate, initialize, and traverse a single-dimensional array in Java.
class B {
public static void main(String args[]) {
int a[] = new int[5]; // Declaration and instantiation
a[0] = 10; // Initialization
a[1] = 20;
a[2] = 70;
a[3] = 40;
a[4] = 50;
// Traversing and printing array elements
for (int i = 0; i < a.length; i++)
System.out.println(a[i]);
}
}
int a[] = new int[5];
→ Declares an integer array with a fixed size of 5.for
loop iterates through the array and prints each element.10
20
70
40
50
Using a single-dimensional array in Java helps efficiently store and manipulate data. Arrays are a fundamental part of Java programming and are widely used in various applications.
For more Java tutorials, stay connected with us on Instagram and Facebook!