简单说说多维数组

  • A+
所属分类:JAVA


共计 1345 个字符,预计需要花费 4 分钟才能阅读完成。

/**
 * @author plm
 * @create 2021/1/10 19:50
 */
public class MultimensionalPrimitiveArrayDemo {
    public static void main(String[] args) {
        int[][] arr = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
        System.out.println(Arrays.deepToString(arr)); // [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

        double[][][] doubles = new double[3][1][2];
        System.out.println(Arrays.deepToString(doubles)); // [[[0.0, 0.0]], [[0.0, 0.0]], [[0.0, 0.0]]]
        String[][][] strings = new String[2][2][2];
        System.out.println(Arrays.deepToString(strings)); // [[[null, null], [null, null]], [[null, null], [null, null]]]
    }
}
基本类型数组的值,在不进行显示初始化时,会被自动初始化;
对象数组会被初始化为null;
/**
 * @author plm
 * @create 2021/1/10 19:50
 */
public class MultimensionalPrimitiveArrayDemo {
    public static void main(String[] args) {
        Random random = new Random(47);
        int[][][] arrs = new int[random.nextInt(10)][][];
        for (int i = 0; i < arrs.length; i++) {
            arrs[i] = new int[random.nextInt(10)][];
            for (int j = 0; j < arrs[i].length; j++) {
                arrs[i][j] = new int[random.nextInt(10)];
            }
        }
        System.out.println(Arrays.deepToString(arrs));
        // [[[0, 0, 0], [0], [0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]], [], [[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]], [[0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0], [], [0, 0, 0, 0, 0, 0, 0, 0]], [[], [0], [0, 0], [0, 0, 0, 0], [0, 0, 0], [0, 0, 0, 0, 0, 0]], [[], [0, 0], [0, 0, 0, 0], [0, 0, 0, 0], []], [[0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], []], [[0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0]]]
    }
}
  • 我的微信
  • 这是我的微信扫一扫
  • weinxin
  • 我的微信公众号
  • 我的微信公众号扫一扫
  • weinxin

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: