JAVA基础
二维数组
1.初始化二维数组
public class code24 {
public static void main(String[] args) {
//初始化二维数组,
int[][] a = new int[2][3]; //有两个一维数组,每个一维数组的长度是3
a[1][2] = 5; //可以直接访问一维数组,因为已经分配了空间
//只分配了二维数组
int[][] b = new int[2][]; //有两个一维数组,每个一维数组的长度暂未分配
b[0] =new int[3]; //必须事先分配长度,才可以访问
b[0][2] = 5;
//指定内容的同时,分配空间
int[][] c = new int[][]{
{1,2,4},
{4,5},
{6,7,8,9}
};
}
}
2.练习,找二维数组中的最大值
public class code25 {
public static void main(String args[]){
int[][] c = {
{1,2,4},
{4,5,10},
{6,7,8,9}
};
int max=1;
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
if(c[i][j]>max)max=c[i][j];
}
}
System.out.println(max);
}
}