Declaring and Initializing Array

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
package com.spy.javaforlearn;
 
 
public class Simple 
{
 
	public static void main(String[] args) {
 
 
	// Declaring int Array of size 3
 
	  int[] number=new int[3];
 
 
	//getting size of Array
 
          System.out.println("Size of Array: "+number.length); 
 
        //initializing Array
 
          number[0]=10;
          number[1]=15;
          number[2]=20;
 
 
        //accessing array element
 
         System.out.println("accessing array element");
 
       for(int i=0;i<number.length;i++)
       {
    	   System.out.println(number[i]);	 
       }
 
    }	
}

output:

1
2
3
4
5
Size of Array: 3
accessing array element
10
15
20

Leave a Reply

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