Understanding Array using Simple program

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com.spy.javaforlearn;
/*
 * -->>ARRAY
 * --Array is collection of element.
 * Java provides a data structure, the array, which stores a fixed-size 
 * sequential collection of elements of the same type. An array is used to 
 * store a collection of data, but it is often more useful to think of an
 *  array as a collection of variables of the same type.
 * 
 */
 
public class Array 
{
 
	public static void main(String[] args)
	{
 
		int b[]=new int[4];
		 b[0]=12;
		 b[1]=13;
		 b[2]=14;
		 b[3]=12;
 
     //	 b[4]=12;  //error array index out of bound
 
		System.out.println("Retrieving Array b Element");
		for(int i=0;i<4;i++)
		{
			System.out.println(b[i]);
		}
 
     System.out.println("*******************************");
 
    //example of static array	
 
	 int a[]=new int [5];
 
		a[0]=12;
		a[2]=34;
 
		//here we only define  1st and 3rd element
		//in other index it print default value
 
		System.out.println("Retrieving array a");
		for(int i=0;i<5;i++)
		{
			System.out.println(a[i]);
		}
 
	}
 
}

Output:

1
2
3
4
5
6
7
8
9
10
11
12
Retrieving Array b Element
12
13
14
12
*******************************
Retrieving array a
12
0
34
0
0

Leave a Reply

Your email address will not be published. Required fields are marked *