Array Index always start with 0 position.
Example 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | public class Ex1{ public static void main(String[] args) { int numbers[]; numbers = new int[6]; numbers[0] = 30; numbers[1] = 25; numbers[2] = 20; numbers[3] = 15; numbers[4] = 5; for(int x=0; x<5; x++) { System.out.println("Numbers " +(x+1) + " is " + numbers[x]) ; } } } |
Output
Numbers 1 is 30
Numbers 2 is 25
Numbers 3 is 20
Numbers 4 is 15
Numbers 5 is 5
Example 2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | public class Ex2{ public static void main(String[] args) { int groups[]; groups = new int[10]; groups[0] = 30; groups[1] = 25; groups[2] = 20; groups[3] = 15; groups[4] = 5; groups[5] = 23; groups[6] = 48; for(int a=0; a<7; a++) { System.out.println("Group " +(a+1)+ "=" + groups[a]); } } } |
Output
Group 1=30
Group 2=25
Group 3=20
Group 4=15
Group 5=5
Group 6=23
Group 7=48
Example 3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | public class Ex3 { public static void main(String[] args) { String names[]; names = new String[4]; names = new String[4]; names[0] = "John"; names[1] = "James"; names[2] = "Steve"; names[3] = "Hume"; for(int x=0; x<4; x++) { System.out.println(names[x]); } } } |
Output
John
James
Steve
Hume
Example 4
1 2 3 4 5 6 7 8 9 10 11 12 13 | public class Ex4{ public static void main(String[] args) { String names[] = {"John","James","Jap","Sam"}; for(int x=0; x<4; x++) { System.out.println(names[x]); } } } |
Example 5
1 2 3 4 5 6 7 8 9 10 11 12 | public class Ex5{ public static void main(String[] args) { int groups[] = {10,20,30,40,50,60,70,80,90,100}; for(int x=0; x<10; x++) { System.out.println(groups[x]); } } } |
10
20
30
40
50
60
70
80
90
100
Example 6
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public class Ex6{ public static void main(String[] args) { String names[] = {"John","Jim","Peter","Anne","James"}; int ages[] ={15,20,23,21,28}; for(int x=0; x<5; x++) { System.out.println(names[x] + " is " + ages[x] + " Years Old" ); } } } |
John is 15 Years Old
Jim is 20 Years Old
Peter is 23 Years Old
Anne is 21 Years Old
James is 28 Years Old
Two Dimensional Arrays
Example 1
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 | public class Ex1{ public static void main(String[] args) { String names[] = {"John","James"}; String courses[] = {"Java","Asp","Php","Python","NodeJS"}; int marks[][] = new int[2][5]; marks[0][0]=75; marks[0][1]=78; marks[0][2]=75; marks[0][3]=95; marks[0][4]=97; marks[1][0]=85; marks[1][1]=88; marks[1][2]=75; marks[1][3]=65; marks[1][4]=88; System.out.println("John"); for(int row=0; row<1; row++) for(int col=0; col<5; col++) System.out.println(courses[col]+ "=" + marks[row][col]); System.out.println("_________________________________"); System.out.println("James"); for(int row=1; row<2; row++) for(int col=0; col<5; col++) System.out.println(courses[col]+ "=" + marks[row][col]); } |
Output
John
Java=75
Asp=78
Php=75
Python=95
NodeJS=97
_________________________________
James
Java=85
Asp=88
Php=75
Python=65
NodeJS=88
Example 2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | public class Ex2{ public static void main(String[] args) { String names[] = {"John","James"}; String courses[] = {"Java","Asp","Php","Python","NodeJS"}; int marks[][] = {{75,78,75,95,97},{85,88,75,65,88}}; System.out.println("John"); for(int row=0; row<1; row++) for(int col=0; col<5; col++) System.out.println(courses[col]+ "=" + marks[row][col] ); System.out.println("_________________________"); System.out.println("James"); for(int row=1;row<2;row++) for(int col=0; col<5; col++) System.out.println(courses[col]+ "=" + marks[row][col]); } } |
Example 3
1 2 3 4 5 6 7 8 9 | public class Ex3{ public static void main(String[] args) { int students[]={10,20,30,40}; System.out.println("Number of classes= " +students.length); } } |
Example 4
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 | public class Ex4{ public static void main(String[] args) { String exams[] = {"Anne","Peter"}; String courses[]={"Java","Asp","Php","Python","NodeJS"}; int marks[][]={{75,85,95,65,55},{55,65,85,75,90}}; System.out.println("Anne"); for(int row=0; row<1; row++) for(int col=0; col<1; col++) System.out.println(courses[col]+ " =" + marks[row][col]); System.out.println("-----------------------------------"); System.out.println("Peter"); for(int row=1; row<2; row++) for(int col=0; col<5; col++) System.out.println(courses[col]+ " =" + marks[row][col]); System.out.println("Number of Subject " + marks[0].length); System.out.println("Number of Names " + marks.length); } } |
Output
Anne
Java =75
———————————–
Peter
Java =55
Asp =65
Php =85
Python =75
NodeJS =90
Number of Subject 5
Number of Names 2
Example 5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | public class Ex5{ public static void main(String[] args) { String courses[] = {"Java","Asp","Php","Python","NodeJS"}; String Student[] = {"John","James","Peter","James","Sam","Steve","Jok","Jack"}; int Marks[][] ={{75,78,75,95,97},{85,88,75,65,88},{45,68,75,65,58}, {55,58,75,85,38}, {95,88,95,90,45},{75,68,45,75,28},{95,78,85,95,88}, {75,68,45,65,88}}; System.out.println("Number of Students = " + Marks.length); System.out.println("Number of Courses = " + Marks[0].length); } } |
Output
Number of Students = 8
Number of Courses = 5