2008年12月5日 星期五

simple java(九)---array(二)

Declaring double-subscripted array

intb[][] = { { 1, 2}, { 3, 4} };

例一.
public class ShowArray {
public static void main(String[] args) {
int array[][] = { { 1, 2, 3 }, { 4, 5, 6 } };
System.out.println("Values in array by row are");
for (int row = 0; row <.array.length;row++){
for (int col = 0; col <. array[row].length; col++) {
System.out.print(array
[row][col] + " ");}
System.out.println();}}}
執行結果:
Values in array by row are
1 2 3
4 5 6
例二.
public class ArrayCal {
public static void main(String[] args) {
int grades[][] = { { 77, 68, 86, 73 },
{ 96, 87, 89, 81 },
{ 70, 90, 86, 81 } };
int students, exams;
String output = "";
students = grades.length;
exams = grades[0].length;
output += " ";
for (int counter = 0; counter <. exams; counter++)
output += "[" + counter + "] ";
for (int row = 0; row <. students; row++) {
output += "\nStudent " + row + ": ";
for (int column = 0; column <. exams; column++)
output += grades[row][column] + " ";}
int highGrade;
for (int row = 0; row <. students; row++)
highGrade = grades[row][0];
for (int column = 0; column <. exams; column++)
if (grades[row][column] > highGrade)
highGrade = grades[row][column];
}
output += "\nThe highest Grade of Student " +row + " is: "
+ highGrade;}
System.out.println(output);
執行結果:
[0] [1] [2] [3]
student0:77 68 86 73
student1:96 87 89 81
student2:70 90 86 81
The highest Grade of Student 0 is:86
The highest Grade of Student 1 is: 96
The highest Grade of Student 2 is: 90

沒有留言: