Programming Example
Example of single dimensional java array
Example of single dimensional java array
public class ArrayExample {
public static void main(String args[]){
int a[]=new int[5];//declaration and instantiation
a[0]=10;//initialization index 0
a[1]=20;//initialization index 1
a[2]=30;//initialization index 2
a[3]=40;//initialization index 3
a[4]=50;//initialization index 4
//printing array
for(int i = 0; i < a.length; i++)//length is the property of array
System.out.println(a[i]);
}
}
10 20 30 40 50 Press any key to continue . . .
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.
After understanding this example, try to rewrite the same program without looking at the code. Then change some values or logic and run it again. This helps improve confidence and keeps learners engaged on the page for longer.