二维数组的运用

二维数组的应用及规范


二维数组的语法的定义

  • 数组类型[][] 数组名 = new 数组类型[一维数组的个数][每一个一维数组中元素的个数]
    例如:
//写法一:第一个中括号决定行数,第二个决定列数
int[][] arraysI ={{1,2,3},{4,5,6},{7,8,9}};
//写法二:
Object[][] arraysO = new Object[][]{
							{"明日花绮罗",18,"日本"},
							{"三上悠亚",21,"日本"},
							{"深田咏美",22,"日本"},//这个逗号加不加都行
};

二维数组与内存的占用
int[][] array = new int [3][4]
在这里插入图片描述


二维数组的遍历

	Object[][] arraysO = new Object[][]{
						{"明日花绮罗",18,"日本"},
						{"三上悠亚",21,"日本"},
						{"深田咏美",22,"日本"},//这个逗号加不加都行
	};
	
//双重for循环
		for(int i =0;i<arraysO.length;i++) {
			for(int j =0;j<arraysO[i].length;j++) {
				System.out.print(arraysO[i][j]+" ");
			}
			System.out.println();
		}
		
//forEach循环
		for(Object[] i:arraysO) {
			for(Object j :i ) {
				System.out.print(j+" ");
			}
				System.out.println();
		}

二维数组的应用(走迷宫)

package Test;

import java.util.Scanner;

public class Array {
	//主函数
	 public static void main(String[] args) {
	        //有八个一维数组,每个一维数组中有十个元素
	        //array[0][0]
	        char[][] array = {
	                        //y   0   1   2   3   4   5   6   7   8   9
	                            {'#','#','#','#','#','#','#','#','#','#'},//0 ----- x
	                            {'#','0',' ',' ',' ',' ','#','#',' ',' '},//1
	                            {'#','#',' ','#','#','#','#','#',' ','#'},//2
	                            {'#',' ',' ',' ',' ','#','#','#',' ','#'},//3
	                            {'#',' ','#','#',' ','#',' ',' ',' ','#'},//4
	                            {'#',' ','#','#',' ','#',' ','#','#','#'},//5
	                            {'#',' ',' ',' ',' ',' ',' ','#','#','#'},//6
	                            {'#','#','#','#','#','#','#','#','#','#'} //7
	                        };
	        while(true) {
	            printMaze(array);//绘制地图,调用第40行方法
	            find0(array);//调用第62行方法
	            String string=input();//调用第50行方法
	            if(string.equals("输入错误")) {
	                System.out.println(string);
	            }else {
	                move(string, array);
	            }
	            if(x == 1&&y==9) {
	                break;
	            }
	        }
	        System.out.println("你赢了");
	        sc.close();
	    }
	 
	 
	    //打印迷宫(传入迷宫)
	    public static void printMaze(char[][] array) {
	        for(int i=0;i<array.length;i++) {
	            for(int j=0;j<array[0].length;j++) {
	                System.out.print(array[i][j]);
	            }
	            System.out.println();
	        }
	    }
	    
	    //接受键盘输入,做判断传出wasd或者输入错误
	    public static String input() {
	    	//next 和 nextLine的区别: next只吸取 字符,nextLine 不但吸取字符还可以吸取空格 和 占位符
	        String ch = sc.nextLine();
	        if(ch.equals("a")||ch.equals("d")||ch.equals("w")||ch.equals("s")) {
	            return ch;
	        }else {
	            return "输入错误";
	        }
	    }
	    static Scanner sc =new Scanner(System.in);
	    static int x=0,y=0;//静态变量存储0的坐标
	    //传数组进去,找出0的坐标,存给静态变量x,y
	    public static void find0(char[][]array) {
	        for (int i = 0; i < array.length; i++) {
	            for (int j = 0; j < array[0].length; j++) {
	                if(array[i][j]=='0') {
	                    x=i;
	                    y=j;
	                }
	            }
	        }
	    }
	    
	    
	    
	    
	    //将经过判断之后的键盘值传入(限定了wasd),根据输入操作数组
	    //移动时要注意移动的方向不是#,如果是墙不移动
	    public static void move(String string,char[][]array) {
	        if (string.equals("a")&&array[x][y-1]!='#') {
	            char temp = array[x][y-1];
	            array[x][y-1] = array[x][y];
	            array[x][y] = temp;
	        }
	        if (string.equals("s")&&array[x+1][y]!='#') {
	            char temp = array[x+1][y];
	            array[x+1][y] = array[x][y];
	            array[x][y] = temp;
	        }
	        if (string.equals("d")&&array[x][y+1]!='#') {
	            char temp = array[x][y+1];
	            array[x][y+1] = array[x][y];
	            array[x][y] = temp;
	        }
	        if (string.equals("w")&&array[x-1][y]!='#') {
	            char temp = array[x-1][y];
	            array[x-1][y] = array[x][y];
	            array[x][y] = temp;
	        }

	    }
} 

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/llp705/article/details/107424089