/* */ Click to Join Live Class with Shankar sir Call 9798158723

One Dimensional Array in Java.


A single dimensional array (also called one dimensional array) is a linear list consisting of related data items of same type . In memory all the data items are stored in contiguous memory locations one after the other.

One dimensional array represents one row or one column of array elements that share a common name and is distinguishable by index values.


Initialization of single dimensional array.

Array can be initialized at the time of declaration when their initial value are known in advance array elements can be initialized with data items of type integer , character , float etc .the value are copied into array in the order specified in the declaration.

Example :-
int arr[5]={ 5, 7, 8, 4, 3 };
Initialization without size
int arr[ ]={4, 6, 2, 3 , 7};
Initialization with a string
char arr[ ]=“computer”;
Assigning values to array
Int a[5];
a[3]=30;

Q.) Example of Single dimensional Array.
   

    class Main {
 public static void main(String[] args) {
  
   // create an array
   int[] age = {12, 4, 5, 2, 5};

   // access each array elements
   System.out.println("Accessing Elements of Array:");
   System.out.println("First Element: " + age[0]);
   System.out.println("Second Element: " + age[1]);
   System.out.println("Third Element: " + age[2]);
   System.out.println("Fourth Element: " + age[3]);
   System.out.println("Fifth Element: " + age[4]);
 }
}
Output
Accessing Elements of Array:
First Element: 12
Second Element: 4
Third Element: 5
Fourth Element: 2
Fifth Element: 5