第7章多维数组Liang,IntroductiontoJava Programming,Eighth Edition,(c)2011Pearson Education,Inc.Allrightsreserved.0132130807
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 1 第7章 多维数组
动因目前,你已经使用过一维数组来对线性的元素集合进行建模。你可以使用二维数组来表示矩阵或表格。例如:使用二维数组可以存储下面这个描述城市之间距离的表格。DistanceTable(inmiles)ChicagoNewYorkAtlantaMiamiDallasHoustonBostonChicago098378771413759671087Boston021498311021763172318422140NewYork787888154915481627Atlanta71411028880661781810013751763154966114261187Miami0967172315487811426239Dallas01087184216278101187239HoustonLiang,Introduction to JavaProgramming,EighthEdition,(c)2011Pearson Education,Inc.Allrightsreserved.0132130807
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 2 动 因 Chicago Boston New York Atlanta Miami Dallas Houston Distance Table (in miles) Chicago Boston New York Atlanta Miami Dallas Houston 0 983 787 714 1375 967 1087 983 0 214 1102 1763 1723 1842 787 214 0 888 1549 1548 1627 714 1102 888 0 661 781 810 1375 1763 1549 661 0 1426 1187 967 1723 1548 781 1426 0 239 1087 1842 1627 810 1187 239 0 1723 1548 781 1426 0 239 目前,你已经使用过一维数组来对线性的元素集合进 行建模。你可以使用二维数组来表示矩阵或表格。例 如:使用二维数组可以存储下面这个描述城市之间距 离的表格
学习目标给出使用二维数组表示数据的例子(第7.1节)。声明二维数组变量、创建数组以及使用行下标和列下标访问二维数组中的数组元素(第7.2节)。编程实现常用的二维数组操作(显示数组、对所有元素求和、找出最小元素和最大元素以及随意打乱数组)(第7.3节)。给方法传递二维数组(第7.4节)(第7.5节)使用二维数组编写多选题评分程序使用二维数组解决距离最近点对问题(第7.6节)。(第7.7节使用二维数组检测一种九宫格的解决方案使用多维数组(第7.8节)。Liang,Introduction toJava Programming.EighthEdition,(c)2011Pearson Education,Inc.Allrightsreserved.0132130807
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 3 学习目标 给出使用二维数组表示数据的例子(第7.1节)。 声明二维数组变量、创建数组以及使用行下标和列下 标访问二维数组中的数组元素(第7.2节)。 编程实现常用的二维数组操作(显示数组、对所有元 素求和、找出最小元素和最大元素以及随意打乱数组 )(第7.3节)。 给方法传递二维数组 (第7.4节)。 使用二维数组编写多选题评分程序(第7.5节)。 使用二维数组解决距离最近点对问题 (第7.6节)。 使用二维数组检测一种九宫格的解决方案(第7.7节) 使用多维数组(第7.8节)
声明/创建二维数组变量//声明数组引用变量dataType[][] refvar创建数组并将它的引用赋值给变量refVar = new dataType[l0][l0];//声明和创建数组合放在一条语句中dataType[][] refvar = newdataType[10][10];//还可选择的语法dataType refVar[][]dataType[10][10];=newLiang,Introduction toJava Programming.EighthEdition,(c)2011Pearson Education,Inc.Allrightsreserved.0132130807
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 4 声明/创建二维数组变量 // 声明数组引用变量 dataType[][] refvar // 创建数组并将它的引用赋值给变量 refVar = new dataType[10][10]; //声明和创建数组合放在一条语句中 dataType[][] refVar = new dataType[10][10]; //还可选择的语法 dataType refVar[][] = new dataType[10][10];
声明二维数组变量和创建二维数组int[][]int[10][10];matrix=neworint matrix[l[]int[10][10];=newmatrix[0][0] = 3;for (int i = O; i < matrix.length; i++)for(int j = O;< matrix[i].length; j++)(int)(Math.random() * 1oo0);matrix[i][j]double[][] x;Liang,Introduction toJava Programming.EighthEdition,(c)2011Pearson Education,Inc.Allrightsreserved.0132130807
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 5 声明二维数组变量 和创建二维数组 int[][] matrix = new int[10][10]; or int matrix[][] = new int[10][10]; matrix[0][0] = 3; for (int i = 0; i < matrix.length; i++) for (int j = 0; j < matrix[i].length; j++) matrix[i][j] = (int)(Math.random() * 1000); double[][] x;