二维数组和方法简介

1. 二维数组

  • 二维数组可能不规则,可能规则,不一定。
  • 定义
    • 数据类型[][] a;
      int a[][];
      int[] a[]; //不常用
  • 创建数组
    a = new int[2][3]; //两行三列,规则的二维表
    a = new int[2][]; //正确
    a = new int[][3]; //错误
public class Demo01 {
	public static void main(String[] args) {
		int[][]	a = new int[2][3]; //两行三列,规则的二维表
		a = new int[2][];  //正确
		a[0][0] = 1; //运行时异常
	}
}

public class Demo01 {
	public static void main(String[] args) {
		//打印杨辉三角
		int[][] a = new int[2][];  //正确
		a[0] = new int[1];
		a[1] = new int[2];
		a[0][0] = 1; 
	}
}
  • 使用数组
public class Demo01 {
	public static void main(String[] args) {
		//打印杨辉三角
		int[][] a = new int[2][];  //正确
		a[0] = new int[1];
		a[1] = new int[2];
		a[0][0] = 1; 
		//二维数组名.length取的长度是行
		for(int i=0;i<a.length;i++){
			for(int j=0;j<a[i].length;j++){
				System.out.print(a[i][j]);
			}
			System.out.println();
		}
		System.out.println(Arrays.toString(a[0]));
		System.out.println(Arrays.toString(a[1]));
	}
}
  • 给数组赋值
	int[][] arr = {{1,2,3},{4,5,6},{1,2}};
	int[][] arr1 = new int[][]{{1,2,3},{4,5,6}};
  • 二维数组原理
    内存结构图: https://blog.csdn.net/songshiMVP1/article/details/49388413
		int[][] arr = {{1,2,3},{4,5,6},{1,2}};
		int[][] arr1 = new int[][]{{1,2,3},{4,5,6}};
		
		System.out.println(a);
		System.out.println(a[0]);
		System.out.println(a[1]);

运行结果:
[[I@15db9742
[I@6d06d69c
[I@7852e922

  • 练习题
    1.求对角线数字的和
/*
 *  1 5 6
 *  7 8 9
 *  1 2 3
 * 
 *  1 2 3 5 
 *  4 3 2 1
 *  4 6 9 7
 *  1 2 7 1
 *  
 *  1 2 3 5 9
 *  4 3 2 1 4
 *  4 6 9 7 7
 *  1 2 7 1 3
 *  1 2 7 1 3
 */
public class Demo2 {
	
	public static void main(String[] args) {
		int[][] arr = {{1,2,3,5},{4,3,2,1},{4,6,9,7},{1,2,7,1}};
		int[][] arr1 = {{1,2,3,5,9},{4,3,2,1,4},{4,6,9,7,7},{1,2,7,1,3},{1,2,7,1,3}};
		int[][] arr2 = {{1,5,6},{7,8,9},{1,2,3}};
		test(arr);
		test(arr1);
		test(arr2);
	}

	private static void test(int[][] arr) {
		int sum = 0;
		for(int i = 0; i < arr.length; i++){
			for(int j = 0;j < arr[i].length; j++){
				if(i==j || i+j == arr.length-1){
					sum+=arr[i][j];
				}
			}
		}
		System.out.println(sum);
	}
}

2.转置矩阵

/*
 *  1 5 6
 *  7 8 9
 *  1 2 3
 */
public class Demo4 {
	public static void main(String[] args) {
		int[][] arr1 = {{1,2,3},{4,5,6}};
		int[][] arr3 = new int[3][2];
		for(int i =0;i<arr1.length;i++){
			for(int j=0;j<arr1[i].length;j++){
				arr3[j][i] = arr1[i][j];
			}
		}
		for(int i=0;i<arr3.length;i++){
			System.out.println(Arrays.toString(arr3[i]));
		}
		int[][] arr2 = {{1,5,6},{7,8,9},{1,2,3}};
		test(arr2);
		for(int i=0;i<arr2.length;i++){
			System.out.println(Arrays.toString(arr2[i]));
		}
	}
	private static void test(int[][] arr) {
		for(int i=0;i<arr.length;i++){
			for(int j = i;j<arr[i].length;j++){
				if(i==j)
					continue;
				else{
					int temp = arr[i][j];
					arr[i][j] = arr[j][i];
					arr[j][i] = temp; 
				}
			}
		}
		
	}
}

在这里插入图片描述
3.杨辉三角

public class Demo3 {
	
	public static void main(String[] args) {
		Scanner sc =  new Scanner(System.in);
		System.out.println("请输入:");
		int i = sc.nextInt();
		test(i);
	}

	private static void test(int i) {
		int[][] arr = new int[i][]; 
		arr[0] = new int[]{1};
		arr[1] = new int[]{1,1};
		for(int j = 2; j < i; j++){
			arr[j] = new int[j+1];
			arr[j][0] = 1;
			arr[j][j] = 1;
			for(int m =1;m<arr[j].length-1;m++){
				arr[j][m] = arr[j-1][m]+arr[j-1][m-1]; 
			}
		}
		for(int n =0;n<arr.length;n++){
			System.out.println(Arrays.toString(arr[n]));
		}
	}
}

2. 方法

  • 方法定义格式
    访问修饰符 方法的返回类型 方法名称(形参列表){
    方法体;
    }

    • 方法修饰符:private default protected public
    • 方法的返回类型:无返回值类型:void 可以有return语句
      有返回值类型:int int[] Object 必须有return语句 并返回兼容的值
    • 方法名称:见名知其义;合法标志符
    • 形参列表:可以有可以没有
  • 使用方法

    • 实参列表的参数传递:、
      基本数据类型:传值
      引用类型:传地址值(也叫值传递,java中没有地址传递一说)
    public class Demo6 {
    	
    	
    	public static void main(String[] args) {
    		int i = 10;
    		test(i);
    		System.out.println(i); //输出10
    		
    		int[] a = new int[3];
    		a[2] = 100;
    		test2(a);
    		System.out.println(a[2]); //输出100
    	}
    
    	public static void test2(int[] a){
    		a = new int[3]; //没有这句输出的就是10
    		a[2] = 10;
    	}
    	private static void test(int i) {
    		i = 9;
    	}
    }
    
    
  • 方法重载:在一个类或者继承关系中,方法名相同,参数列表不同(个数不同;类型不同;顺序不同)。方法的重载和方法的返回类型无关
    在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值