获取二维数组的长度 [英] Getting the length of two-dimensional array

查看:30
本文介绍了获取二维数组的长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我不知道如何获得数组的第二维?array.length 只给出第一个维度.

How do I get the second dimension of an array if I don't know it? array.length gives only the first dimension.

例如,在

public class B {
    public static void main(String [] main){
        int [] [] nir = new int [2] [3];
        System.out.println(nir.length);
    }
}

我如何获得 nir 的第二个维度的值,即 3.

how would I get the value of the second dimension of nir, which is 3.

谢谢

推荐答案

which 3?

您已经创建了一个多维数组.nir 是一个 int 数组的数组;你有两个长度为 3 的数组.

You've created a multi-dimentional array. nir is an array of int arrays; you've got two arrays of length three.

System.out.println(nir[0].length); 

会给你你的第一个数组的长度.

would give you the length of your first array.

另外值得注意的是,您不必像以前那样初始化多维数组,这意味着所有数组不必具有相同的长度(或根本不存在).

Also worth noting is that you don't have to initialize a multi-dimensional array as you did, which means all the arrays don't have to be the same length (or exist at all).

int nir[][] = new int[5][];
nir[0] = new int[5];
nir[1] = new int[3];
System.out.println(nir[0].length); // 5
System.out.println(nir[1].length); // 3
System.out.println(nir[2].length); // Null pointer exception

这篇关于获取二维数组的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆