Arrays

Java Array:

● Array is a collection of similar type of elements that have contiguous memory location.
● Java array is an object the contains elements of similar data type.
● It is a data structure where we store similar elements.
● We can store only fixed set of elements in a java array.
● Array in java is index based, first element of the array is stored at 0 index.

Array Declaration:

Syntax:
datatype[] identifer;
or
datatype identifer[];

Types of array in java:

There are two types of array.
● Single Dimensional Array
● Multidimensional Array

Initialization of Array:

Example:
int arr[] = new int [10];
or
int arr[] = {10,20,30,40,50};
new operator is used to initialize an array.

Array Example:

/*java Array Example*/
public class TestArray 
{
   public static void main(String[] args)
   {
     int arr[] = {10,20,30,40,50}; 
     System.out.println("Array[0]="+arr[0]); 
     System.out.println("Array[1]="+arr[1]);
System.out.println("Array[2]="+arr[2]);
System.out.println("Array[3]="+arr[3]);    
System.out.println("Array[4]="+arr[4]);   
    }
}

Output:
Array[0]=10
Array[1]=20
Array[2]=30
Array[3]=40
Array[4]=50