In a two-dimensional array, declare two [] as [] [].
sample
int[][] score = {
{ 1, 2, 3, 4, 5 }, //Line 0
{ 10, 20, 30, 40, 50 }, //The first line
{ 100, 200, 300, 400, 500 } //2nd line
};
System.out.println(score[Line][Column]);
System.out.println(score[0][0]); //0th row, 0th column
System.out.println(score[1][1]); //1st row, 1st column
System.out.println(score[2][3]); //2nd row, 3 columns
//Note that it starts at row 0, column 0
int[][] array = {
{1,2,3,4},
{10,20,30,40,50,60},
{1000,10000}
};
//Outputs all integer elements contained in the 2D array array in order
for(int i=0; i<array.length; i++) { //Array name.Get the number of elements in a column (vertical direction) by length
for(int s=0; s<array[i].length; s++) { //Array name[i].Get the number of elements in a row (horizontal direction) by length
System.out.println(array[i][s]); //array[Vertical][side]
}
}
Console results 1 2 3 4 10 20 30 40 50 60 1000 10000
sample
int sum = 0;
for(int i=0; i<array.length; i++) {
for(int s=0; s<array[i].length; s++) {
sum += array[i][s];
}
}
System.out.println(sum);
}
Console results 11220
sample
int sum = 0;
int count = 0;
for(int i=0; i<array.length; i++) {
for(int s=0; s<array[i].length; s++) {
sum += array[i][s];
count++;
}
}
System.out.println(sum/count);
}
Console results 935
Recommended Posts