Introduction
- Each element is a two-dimensional array of one-dimensional array
- Data Type [] [] = new Array name Data type [two-dimensional array of length (number of rows)] [length (number of columns) of each one-dimensional array of]
- Access the array using the array name [Industry Standard] [column header]
Create a two-dimensional array
Create a way
// 申明创建方式和一维数组类似
// 申明时直接赋值
数据类型[][] 数组名 = {{元素},{元素1, 元素2},……};
// 只申明数组的长度不赋值
// 三行三列
int [][] a = new int[3][3];
Applications
Case number one
package JavaNote001;
import java.util.Arrays;
public class ArrayList005 {
public static void main(String[] args) {
// 申明数组并赋值
int [][] s = {{1,2,3},{4,5,6},{7,8,9}};
// 打印数组的值:使用双重for循环
for (int i=0;i<s.length;i++){
for (int j=0;j<s[i].length;j++){
System.out.print(s[i][j]+" ");
}
System.out.println();
}
}
}
/*
superme@ubuntu:~/JavaSE$ cd /home/superme/JavaSE ; /usr/local/jdk1.8/bin/java -Dfile.encoding=UTF-8 -cp /home/superme/.config/Code/User/workspaceStorage/fa5e4cfe4092c2e37bbba6cdb539eaab/redhat.java/jdt_ws/JavaSE_d995f1f1/bin JavaNote001.ArrayList005
1 2 3
4 5 6
7 8 9
superme@ubuntu:~/JavaSE$
*/
Case II
// 使用二维数组模仿五子棋落子
import java.util.Scanner;
public class ArrayPractice {
public static void main(String[] args) {
init();
serial(init());
print(playChess(init()));
}
//初始化棋盘
public static String[][] init() {
//定义一个二位数组
String [][] qp = new String[15][15];
//给棋盘进行初始化
for(int i=0;i<qp.length;i++) {
for (int j=0;j<qp[i].length;j++) {
qp[i][j] = "+ ";
}
}
return qp;
}
//给棋盘编号方法
public static void serial(String[][] qp) {
//给棋盘编号
String[] num = {"⒈","⒉","⒊","⒋","⒌","⒍","⒎","⒏","⒐","⒑","⒒","⒓","⒔","⒕","⒖"};
for (int i=0;i<qp.length;i++) {
for (int j=0;j<qp[i].length;j++) {
if (i == qp.length-1) {
qp[i][j] = num[j];
}
if (j == qp[i].length-1) {
qp[i][j] = num[i];
}
}
}
//调用输出方法输出
print(qp);
}
//输出方法
public static void print(String [][] qp) {
for(int i=0;i<qp.length;i++) {
for (int j=0;j<qp[i].length;j++) {
System.out.print(qp[i][j]);
}
System.out.println();
}
}
//黑白双方循环落子
public static String[][] playChess(String[][] qp){
String white = "☆ ";
String black = "★ ";
Scanner scanner = new Scanner(System.in);
boolean mark = true;
while(true) {
System.out.println("请"+(mark? "黑":"白")+"棋落子");
//用户输入落子的坐标
String s = scanner.next();
String[] chessPieces = s.split(",");
//将落子的坐标转化为int类型
int x = Integer.parseInt(chessPieces[0])-1;
int y = Integer.parseInt(chessPieces[1])-1;
//判断输入的坐标是否在棋盘内
if (x<0 || y<0 || x>13 || y>13) {
System.out.println("超出棋盘范围落子无效");
}else {
//判断输入坐标是否已有棋子
if(qp[x][y].equals("+ ")) {
if (mark) {
qp[x][y] = black;
mark = false;
// for (int m=0;m<5;m++) {
// if (qp[x+m][y].equals(black) || qp[x][y+m].equals(black) || qp[x-m][y].equals(black) || qp[x][y-m].equals(black) || qp[x+m][y+m].equals(black) || qp[x+m][y-m].equals(black) || qp[x-m][y-m].equals(black) || qp[x-m][y+m].equals(black)) {
// System.out.println("黑方获胜...........");
// break;
// }
// }
}else {
qp[x][y] = white;
// for (int m=0;m<5;m++) {
// if (qp[x+m][y].equals(white) || qp[x][y+m].equals(white) || qp[x-m][y].equals(white) || qp[x][y-m].equals(white) || qp[x+m][y+m].equals(white) || qp[x+m][y-m].equals(white) || qp[x-m][y-m].equals(white) || qp[x-m][y+m].equals(white)) {
// System.out.println("白方获胜...........");
// break;
// }
}
}else {
System.out.println("该处已有棋子,落子无效");
}
}
print(qp);
}
}
}
/*
superme@ubuntu:~/JavaSE/JavaSEPractice001$ java ArrayPractice
+ + + + + + + + + + + + + + ⒈
+ + + + + + + + + + + + + + ⒉
+ + + + + + + + + + + + + + ⒊
+ + + + + + + + + + + + + + ⒋
+ + + + + + + + + + + + + + ⒌
+ + + + + + + + + + + + + + ⒍
+ + + + + + + + + + + + + + ⒎
+ + + + + + + + + + + + + + ⒏
+ + + + + + + + + + + + + + ⒐
+ + + + + + + + + + + + + + ⒑
+ + + + + + + + + + + + + + ⒒
+ + + + + + + + + + + + + + ⒓
+ + + + + + + + + + + + + + ⒔
+ + + + + + + + + + + + + + ⒕
⒈⒉⒊⒋⒌⒍⒎⒏⒐⒑⒒⒓⒔⒕⒖
请黑棋落子
2,4
+ + + + + + + + + + + + + + +
+ + + ★ + + + + + + + + + + +
+ + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + +
请白棋落子
5,6
+ + + + + + + + + + + + + + +
+ + + ★ + + + + + + + + + + +
+ + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + +
+ + + + + ☆ + + + + + + + + +
+ + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + +
请白棋落子
5,6
该处已有棋子,落子无效
+ + + + + + + + + + + + + + +
+ + + ★ + + + + + + + + + + +
+ + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + +
+ + + + + ☆ + + + + + + + + +
+ + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + +
请白棋落子
*/