The simplest form of an array is a single dimensional array.

  • Syntax to Declare an Array in java


    Syntax :

    
    dataType[]  name ; (or)  
    dataType  []name ; (or)  
    dataType  name[];  


    Instantiation of an Array in java

    
    arrayNam= new DataType [ size ] ;


    Declaration and Instantiation of an Array at the same time.

    
    int a[]=new int[5];
    int[] a=new int[5];
    int a[]=new int[5];

    We are creating int type array of size=5.

  • Example of single dimensional array

    Let's see the simple example of java array, where we are going to declare, instantiate, initialize and traverse an array.

    Example)
    drag_handle
     //example of 1D array
    class Example
    {
    	public static void main(String[] args) {
    	//declearing and instantiating int type array
    	int[] a=new int[5];
    
    	//initializing the elements of array
    	a[0]=45;//first element
    	a[1]=54;
    	a[2]=465;
    	a[3]=32;
    	a[4]=55;//last element
    
    	//now going to traverse the elements of array
    	 for(int i=0;i<5;i++)
    	 {
    	 	System.out.println("a["+i+"] = "+a[i]);
    	 }
    	}
    }
    Output:
    a[0] = 45
    a[1] = 54
    a[2] = 465
    a[3] = 32
    a[4] = 55

    Example of Initializing the elements of an array at the time of declaring the array.

    
    //example of 1D array
    class Example {
    
        public static void main(String[] args) {
            /*if we know the values in advance then we can initialize the elements at the time of declearing the array.
             */
            int a[] = {12, 32, 54, 65, 67};
    
            //now going to traverse the elements of array
            int size = a.length;//fetching the size of array
            for (int i = 0; i < size; i++) {
                System.out.println("a[" + i + "] = " + a[i]);
            }
        }
    }
    

    Output:
    a[0] = 12
    a[1] = 32
    a[2] = 54
    a[3] = 65
    a[4] = 67

    In this case, we don't have to give the size explicitly. size=number of elements given.


Program to read marks of 5 students and store them under an array.
drag_handle

//example of reading values from user and putting to array
import java.util.Scanner;
class Example {

    public static void main(String[] args) {
        int marks[]=new int[5];
        //taking input from user and store in array
        Scanner ob=new Scanner(System.in);
        for (int i = 0; i < marks.length; i++) {
            marks[i]=ob.nextInt();
        }        
        //traversing elements of array
        for (int i = 0; i < marks.length; i++) {
            System.out.println("mark of student "+(i+1)+" is "+marks[i]);
        }
    }
}

Output:

34 40 54 34 45
mark of student 1 is 34
mark of student 2 is 40
mark of student 3 is 54
mark of student 4 is 34
mark of student 5 is 45


Program to take 5 elements from the user and sum them.
drag_handle
//finding the sum of elements of array
import java.util.Scanner;

class Example {

    public static void main(String[] args) {
        int marks[] = new int[5];
        //taking input from user and store in array
        Scanner ob = new Scanner(System.in);
        for (int i = 0; i < marks.length; i++) {
            marks[i] = ob.nextInt();
        }

        int sum = 0;
        //adding elements one by one
        for (int i = 0; i < marks.length; i++) {
            sum = sum + marks[i];
        }
        System.out.println("Sum of elements = " + sum);
    }
}

Output:

3 4 3 5 6 
Sum of elements = 21


Example of Passing Array to method in java
drag_handle

//example of passing array to method as prameter
// in this example we reverse the elements of array 
import java.util.Scanner;

class Example {

    public void reverse(int a[]) {
        //we are going to traverse the elements in opposite direct 
        // size-1 to 0
        int size = a.length;
        for (int i = size - 1; i >= 0; i--) {
            System.out.print(a[i] + "\t");
        }
    }

    public static void main(String[] args) {
        int []myarray={1,3,4,5};
        
        /*since the reverse() method is non-static ,we need to create 
        the object of Example class to call the reverse() method  */
        Example e=new Example();
        e.reverse(myarray);//pass myarray as parameter
    }

}

Output:

5	4	3	1	

What next?

Share this page