What is an array in Java?



An array is a data structure/container/object that stores a fixed-size sequential collection of elements of the same type. The size/length of the array is determined at the time of creation.

The position of the elements in the array is called as index or subscript. The first element of the array is stored at the index 0 and, the second element is at the index 1 and so on.

Each element in an array is accessed using an expression which contains the name of the array followed by the index of the required element in square brackets.

For example, if an array of five elements is created with name myArray, you can access the element of the array at index 3 as:

myArray[3]

 

Five Elements

Example

Live Demo

public class ArrayExample {
   public static void main(String args[]){
      //Declaring an array
      int[] myArray = {233, 783, 453};
      
      //Printing the array
      for(int i=0; i<myArray.length; i++){
         System.out.println(myArray[i]);
      }
   }
}

Output

233
783
453

Advertisements