7.1 二维数组的定义
- 声明二维数组变量并创建二维数组
下面是声明二维数组的语法:
数据类型[][]数组名 :
或者
数据类型数组名[][]; // 允许这种方式 , 但并不推荐使用它 - 获取二维数组的长度
二维数组实际上是一个数组, 它的每个元素都是一个一维数组。 数组 X 的长度是数组中元素的个数, 可以用 x.length 获取该值。 元素 x[0], x[l], … ,x[x.length-l] 也是数组。可以使用 x[0].length, x[l].length, … , x[x.length
-1] .length 获取它们的长度。 - 锯齿数组
二维数组中的每一行本身就是一个数组, 因此, 各行的长度就可以不同。 这样的数组称为锯齿数组(ragged array)。
7.2二维数组的处理
- 二维数组的遍历
for(int i=0;i<matrix.length;i++){
for(int j=0;j<matrix[i].length;j++){
System.out.print(matrix[i][j];
}
System.out.printin();
}
- 示例:选择题测试评分
这里我们建了两个文本,Key.txt和answer.txt,将正确答案直接放入Key.txt,将学生作答放入answer.txt中。
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Arrays;
public class Test1 {
public static void main(String[] args) {
String[][] answer=new String[0][0];
String[] key=new String[0];
answer=ReadAnswers();
key=ReadKey();
System.out.println(Arrays.toString(key));
for(int i=0;i<answer.length;i++){
System.out.println(Arrays.toString(answer[i]));
}
int[] fenshu=new int[answer.length];
for(int i=0;i<answer.length;i++){
fenshu[i]=getSoert(answer[i],key);
}
System.out.println(Arrays.toString(fenshu));
}
private static int getSoert(String[] answer, String[] key) {
int count=0;
for(int i=0;i<answer.length;i++){
if(answer[i].equals(key[i])){
count++;
}
}
return count;
}
private static String[] ReadKey() {
String[] keys=null;
try{
BufferedReader br=new BufferedReader(new FileReader(new File("Key.txt")));
String line="";
while((line=br.readLine())!=null){
line=line.trim();
keys=line.split(" ");
}
}catch(Exception e){
e.printStackTrace();
}
return keys;
}
private static String[][] ReadAnswers() {
String[][] answers=new String[0][0];
try{
BufferedReader br=new BufferedReader(new FileReader(new File("answer.txt")));
String line="";
while((line=br.readLine())!=null){
line=line.trim();
answers=Arrays.copyOf(answers, answers.length+1);
answers[answers.length-1]=line.split(" ");
}
}catch(Exception e){
e.printStackTrace();
}
return answers;
}
}
- 示例:找出距离最近的点对
import java.util.Arrays;
public class Test2 {
public static void main(String[] args) {
double minDistance=0;
double[][] aimPoints=new double[][]{
{0,0},
{0,0}
};
double[][] point=new double[][]{
{-1,3},
{-1,-1},
{1,1},
{2,0.5},
{2,-1},
{3,3},
{4,2},
{4,-0.5}
};
for(int i=0;i<point.length-1;i++){
for(int j=i+1;j<point.length;j++){
double Distance=getDistance(point[i],point[j]);
if(i==0&&j==i+1){
minDistance=Distance;
aimPoints[0]=point[i];
aimPoints[1]=point[j];
}
else{
if(minDistance>Distance){
minDistance=Distance;
aimPoints[0]=point[i];
aimPoints[1]=point[j];
}
}
}
}
System.out.println(minDistance);
System.out.println(Arrays.toString(aimPoints[0]));
System.out.println(Arrays.toString(aimPoints[1]));
}
private static double getDistance(double[] p1, double[] p2) {
double deltX=p1[0]-p2[0];
double deltY=p1[1]-p2[1];
return Math.sqrt(Math.pow(deltX, 2)+Math.pow(deltY, 2));
}
}
- 示例:数独
public class Test3 {
public static void main(String[] args) {
//1.每行都必须是1~9
//2.每列都必须是1~9
//3.每个3*3的方块都必须是1~9
int[][] grid={
{5,3,4,6,7,8,9,1,2},
{6,7,2,1,9,5,3,4,8},
{1,9,8,3,4,2,5,6,7},
{8,5,9,7,6,1,4,2,3},
{4,2,6,8,5,3,7,9,1},
{7,1,3,9,2,4,8,5,6},
{9,6,1,5,3,7,2,8,4},
{2,8,7,4,1,9,6,3,5},
{3,4,5,2,8,6,1,7,9}
};
//合法验证
if(isAllRowLegal(grid)&&isAllColLegal(grid)&&isAllCellLegal(grid)){
System.out.println("Legal!");
}else{
System.out.println("Illegal!");
}
}
private static boolean isAllCellLegal(int[][] grid) {
//找基点00 03 06 30 33 36 60 63 66
for(int i=0;i<9;i+=3){
for(int j=0;j<9;j+=3){
boolean[] status=new boolean[9];
//找偏移 00 01 02 10 11 12 20 21 22
for(int x=0;x<3;x++){
for(int y=0;y<3;y++){
if(status[grid[i+x][j+y]-1]==false){
status[grid[i+x][j+y]-1]=true;
}else{
return false;
}
}
}
}
}
return true;
}
private static boolean isAllColLegal(int[][] grid) {
for(int j=0;j<9;j++){
boolean[] status=new boolean[9];
for(int i=0;i<9;i++){
if(status[grid[i][j]-1]==false){
status[grid[i][j]-1]=true;
}else{
return false;
}
}
}
return true;
}
private static boolean isAllRowLegal(int[][] grid) {
//5,3,4,6,7,8,9,1,2
//1 1 1 1
//boolean[] status=new boolean[9];
//0 1 2 3 4 5 6 7 8
for(int i=0;i<grid.length;i++){
boolean[] status=new boolean[9];
for(int j=0;j<grid[i].length;j++){
if(status[grid[i][j]-1]==false){
status[grid[i][j]-1]=true;
}else{
return false;
}
}
}
return true;
}
}
本章小结
1.可以使用二维数组来存储表格。
2.可以使用以下语法来声明二维数组变量:
元素类型[][] 数 组 变 量
3. 可以使用以下语法来创建二维数组变量:
new 元素类型 [ 行的个数 ][ 列的个數 ]
4. 使用下面的语法表示二维数组中的每个元素:
数组变量 [ 行下标 ][ 列下标 ]
5. 可以使用数组初始化语法来创建和初始化二维数组:
元素类型[][] 数组变置 ={{ 某行的值 } { 某行的值 }}
6.可以使用数组的数组构成多维数组。 例如: 一个三维数组变量可以声明为 “ 元素类型[][][]数组变量”, 并使用 “ new 元素类型 [sizel][size2][size3] ” 来创建三维数组。