type array[][]; type[][] array;其中,type 表示二维数组的类型,array 表示数组名称,第一个中括号表示行,第二个中括号表示列。
int[][] age; char[][] sex;
array=new type[][]{值 1,值 2,值 3,…,值 n};
array=new type[][]{new 构造方法(参数列),…};
type[][] array={{第1行第1列的值,第1行第2列的值,…},{第2行第1列的值,第2行第2列的值,…},…};
int[][] temp;
temp=new int[][]
{
{1,2},{3,4}
};
int[][] temp;
temp=new int [][]
{
{new int(1),new int(2)},{new int(3),new int(4)}
};
int[][] temp={{1,2},{3,4}};
array[i-1][j-1];
public static void main(String[] args)
{
double[][] class_score={{10.0,99,99},{100,98,97},{100,100,99.5},{99.5,99,98.5}};
System.out.println("第二行第二列元素的值:"+class_score[1][1]);
System.out.println("第四行第一列元素的值:"+class_score[3][0]);
}
第二行第二列元素的值:98.0 第四行第一列元素的值:99.5
public static void main(String[] args)
{
double[][] class_score={{100,99,99},{100,98,97},{100,100,99.5},{99.5,99,98.5 }};
for(int i=0;i<class_score.length;i++)
{ //遍历行
for(int j=0;j<class_score[i].length;j++)
{
System.out.println("class_score["+i+"]["+j+"]="+class_score[i][j]);
}
}
}
class_score[0][0]=100.0 class_score[0][1]=99.0 class_score[0][2]=99.0 class_score[1][0]=100.0 class_score[1][1]=98.0 class_score[1][2]=97.0 class_score[2][0]=100.0 class_score[2][1]=100.0 class_score[2][2]=99.5 class_score[3][0]=99.5 class_score[3][1]=99.0 class_score[3][2]=98.5
public class Test11
{
public static void main(String[] args)
{
//创建一个二维矩阵
int[][] matrix=new int[5][5];
//随机分配值
for(int i=0;i<matrix.length;i++)
{
for(int j=0;j<matrix[i].length;j++)
{
matrix[i][j]=(int)(Math.random()*10);
}
}
System.out.println("下面是程序生成的矩阵\n");
//遍历二维矩阵并输出
for(int k=0;k<matrix.length;k++)
{
for(int g=0;g<matrix[k].length;g++)
{
System.out.print(matrix[k][g]+"");
}
System.out.println();
}
}
}
34565 96033 48741 10583 63985
public static void main(String[] args)
{
double[][] class_score={{100,99,99},{100,98,97},{100,100,99.5},{99.5,99,98.5}};
Scanner scan=new Scanner(System.in);
System.out.println("当前数组只有"+class_score.length+"行,您想查看第几行的元素?请输入:");
int number=scan.nextInt();
for(int j=0;j<class_score[number-1].length;j++)
{
System.out.println("第"+number+"行的第["+j+"]个元素的值是:"+class_score[number-1][j]);
}
}
当前数组只有4行,您想查看第几行的元素?请输入: 3 第3行的第[0]个元素的值是:100.0 第3行的第[1]个元素的值是:100.0 第3行的第[2]个元素的值是:99.5
public static void main(String[] args)
{
double[][] class_score={{100,99,99},{100,98,97},{100,100,99.5},{99.5,99,98.5}};
Scanner scan=new Scanner(System.in);
System.out.println("您要获取哪一列的值?请输入:");
int number=scan.nextInt();
for(int i=0;i<class_score.length;i++)
{
System.out.println("第 "+(i+1)+" 行的第["+number+"]个元素的值是"+class_score[i][number]);
}
}
您要获取哪一列的值?请输入: 2 第 1 行的第[2]个元素的值是99.0 第 2 行的第[2]个元素的值是97.0 第 3 行的第[2]个元素的值是99.5 第 4 行的第[2]个元素的值是98.5
版权说明:Copyright © 广州松河信息科技有限公司 2005-2025 版权所有 粤ICP备16019765号
广州松河信息科技有限公司 版权所有