二维数组
静态二位数组
理解:二维数组中包含了多个一维数组
声明:
数据类型 变量名;--推荐
数据类型 变量名;
//静态初始化1//String[][] names = new String[][]{{"小红","小绿","小蓝"},{"小黄","小紫","小黑","小粉"}};//静态初始化2//String[][] names;{"小红","小绿","小蓝"},{"小黄","小紫","小黑","小粉"}//names = new String[][]{};//静态初始化3String[][] names = {{"小红","小绿","小蓝"},{"小黄","小紫","小黑","小粉"}};//设置指定下标上的元素names[1][0] = "小明"; //获取指定下标上的元素String str = names[1][0];System.out.println("获取指定下标上的元素:" + str);//获取长度System.out.println("获取二维数组中一维数组的长度:" + names.length);//2System.out.println("获取二维数组中第一个一维数组元素的长度:" + names[0].length);//3System.out.println("获取二维数组中第二个一维数组元素的长度:" + names[1].length);//4 //遍历数组 -- forfor (int i = 0; i < names.length; i++) {for (int j = 0; j < names[i].length; j++) {System.out.println(names[i][j]);}}System.out.println("-------------------------------");//遍历数组 -- foreachfor (String[] ss : names) {for (String element : ss) {System.out.println(element);}}
动态二维数组
//动态初始化1 -- 2表示一维数组,3表示每个一维数组元素的个数//String[][] names = new String[2][3];//动态初始化2 -- 2表示一维数组,3表示每个一维数组元素的个数String[][] names;names = new String[2][3];//设置指定下标上的元素names[0][0] = "小红";names[0][1] = "小黄";names[0][2] = "小绿";names[1][0] = "小蓝";names[1][1] = "小紫";names[1][2] = "小黑";//获取指定下标上的元素String str = names[1][0];System.out.println("获取指定下标上的元素:" + str);//获取长度System.out.println("获取二维数组中一维数组的长度:" + names.length);//2System.out.println("获取二维数组中第一个一维数组元素的长度:" + names[0].length);//3System.out.println("获取二维数组中第二个一维数组元素的长度:" + names[1].length);//3//遍历思路:依次遍历二维数组中的一维数组,再遍历一维数组中的元素System.out.println("-------------------------------"); //遍历数组 -- forfor (int i = 0; i < names.length; i++) {for (int j = 0; j < names[i].length; j++) {System.out.println(names[i][j]);}}System.out.println("-------------------------------");//遍历数组 -- foreachfor (String[] ss : names) {for (String element : ss) {System.out.println(element);}}/*** 扩展:* boolean bool = true; --> boolean值在内存中占4字节,因为底层使用int类型表述* boolean[] bs = {true,false}; --> 数组里的boolean值在内存中占1字节,因为底层使用byte类型表述*/ * 需求:使用动态初始化创建二维数组,二维数组中有两个一维数组,* 第一个一维数组有3个元素,* 第二个一维数组有4个元素public static void main(String[] args) {String[][] names = new String[2][];String[] s1 = {"小红","小绿","小蓝"};String[] s2 = {"小黄","小紫","小黑","小粉"};names[0] = s1;names[1] = s2;for(String[] ss : names){for (String element : ss) {System.out.println(element);}}
数组五子棋
package Test01; import java.util.Scanner; public class Test01 {public static void main(String[] args) {//棋盘长度int length=20;//棋盘容器String[][] gobang=new String[length][length];//棋盘符号String add = "┼";String black = "●";String white = "○";String[] nums = {"⒈","⒉","⒊","⒋","⒌","⒍","⒎","⒏","⒐","⒑","⒒","⒓","⒔","⒕","⒖","⒗","⒘","⒙","⒚","⒛"}; //初始化棋盘for (int i = 0; i < gobang.length; i++) {for (int j = 0; j < gobang[i].length; j++) {if(j==length-1){//每行的最后一列 -- 设置行数gobang[i][j]=nums[i];}else if(i==length-1){//最后一行 -- 设置列数gobang[i][j]=nums[j];}else{gobang[i][j]=add;}}}for(String[] arr : gobang){for(String element : arr){System.out.print(element);}System.out.println();} boolean flag=true;//true表示黑子,false表示白子 Scanner scan = new Scanner(System.in);boolean bool = true;while(bool){//输入坐标System.out.println("请输入"+((flag)?"黑":"白")+"子坐标:");int x=scan.nextInt()-1;int y=scan.nextInt()-1; //判断下标是否超出棋盘范围if(x<0||y>length-2||y<0||y>length-2){System.out.println("落子失败--坐标上已经有棋子");continue;} //落子String piece=(flag)?black:white;gobang[x][y]=piece; //打印棋盘for(String[] arr : gobang){for(String element : arr){System.out.print(element);}System.out.println();} //判断输赢if(method01(gobang, x, y, piece)>=5 || method02(gobang, x, y, piece)>=5 || method03(gobang, x, y, piece)>=5 || method04(gobang, x, y, piece)>=5){System.out.println(((flag)?"黑":"白")+"赢得了比赛");bool=false;} //置反flag=!flag; }//关闭资源scan.close(); } //获取左右方向棋子的连接数public static int method01(String[][] gobang,int x,int y,String piece){ int count = 1; //判断左横向int tempY = y;while(tempY>0){tempY--;if(gobang[x][tempY].equals(piece)){count++;}else{break;}} //判断右横向tempY = y;while(tempY<18){tempY++;if(gobang[x][tempY].equals(piece)){count++;}else{break;}} return count;} //获取上下方向棋子的连接数public static int method02(String[][] gobang,int x,int y,String piece){int count =1;//判断上方int tempX = x;while(tempX>0){tempX--;if(gobang[tempX][y].equals(piece)){count++;}else{break;}} //判断下方tempX = x;while(tempX<18){tempX++;if(gobang[tempX][y].equals(piece)){count++;}else{break;}} return count; } //获取右上/左下方向棋子的连接数public static int method03(String[][] gobang,int x,int y,String piece){int count =1;//判断右上int tempX = x;int tempY = y;while(tempX>0&&tempY<18){tempX--;tempY++;if(gobang[tempX][tempY].equals(piece)){count++;}else{break;}} //判断左下方tempX = x;tempY = y;while(tempX<18&&tempY>0){tempX++;tempY--;if(gobang[tempX][tempY].equals(piece)){count++;}else{break;}} return count;} //获取左上/右下方向棋子的连接数public static int method04(String[][] gobang,int x,int y,String piece){int count =1;//判断左上int tempX = x;int tempY = y;while(tempX>0&&tempY>0){tempX--;tempY--;if(gobang[tempX][tempY].equals(piece)){count++;}else{break;}} //判断右下方tempX = x;tempY = y;while(tempX<18&&tempY<18){tempX++;tempY++;if(gobang[tempX][tempY].equals(piece)){count++;}else{break;}} return count;} }