Home / Programs / An array example in java
Programming Example

An array example in java

👁 5,496 Views
💻 Practical Program
📘 Step by Step Learning
Important of an array example

Program Code

class ArrayDemo{
     public static void main(String args[]){
        int array[] = new int[7];

        for (int count=0;count<7;count++){
           array[count]=count+1;
       }
       
       for (int count=0;count<7;count++){
           System.out.println("array["+count+"] = "+array[count]);
       }

    }
}

Output

array[0] = 1
array[1] = 2
array[2] = 3
array[3] = 4
array[4] = 5
array[5] = 6
array[6] = 7
Press any key to continue . . .

Explanation

Save , Compile & Run the code. Observe the Output. If x is a reference to an array, x.length will give you the length of the array. Unlike C, Java checks the boundary of an array while accessing an element in it. Java will not allow the programmer to exceed its boundary.

How to learn from this program

First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.