Simple arrays

class jamesprogram {

	public static void main(String args[]){
		
		// declare an array, james[] in which 
		// we will store 10 values
		int james[]=new int[10];
		
		james[0]=50;
		james[1]=34;
		james[2]=3;
		
		System.out.println(james[2]);
	}
}

Outputs:

3

Note: new use ‘new’ above because an array is actually an object

class jamesprogram {

	public static void main(String args[]){
		
		// declare an array, james[] in which 
		// we will store 10 values
		int james[]={3,5,4,7,4,3,2,7};
		
		System.out.println(james[2]);
	}
}

Outputs:

4

(the index of arrays starts at zero)

Creating an array table

class jamesprogram {

	public static void main(String args[]){
		
		System.out.println("Index\tValue"); // \t is a tab
		
		int james[]={3,6,5,4,3,7,2,21,5,88};
		
		for (int count=0;count<james.length;count++){
			
			System.out.println(count + "\t" + james[count]);
			
		}
	}
}

Outputs:

Index Value
0 3
1 6
2 5
3 4
4 3
5 7
6 2
7 21
8 5
9 88

Leave a Reply