2010-10-22 52 views
72

我需要获取行和列的二维数组的长度。我已经使用以下代码成功完成了此操作:在Java中获取二维数组的长度

public class MyClass { 

public static void main(String args[]) 
    { 
    int[][] test; 
    test = new int[5][10]; 

    int row = test.length; 
    int col = test[0].length; 

    System.out.println(row); 
    System.out.println(col); 
    } 
} 

按预期打印出5,10。

现在就来看看这条线:

int col = test[0].length; 

请注意,我居然要引用一个特定的行,以获得列的长度。对我来说,这看起来非常难看。此外,如果将阵列定义为:

test = new int[0][10]; 

然后,当尝试获取长度时代码将失败。有没有不同的(更聪明的)方法来做到这一点?

回答

97

考虑

public static void main(String[] args) { 

    int[][] foo = new int[][] { 
     new int[] { 1, 2, 3 }, 
     new int[] { 1, 2, 3, 4}, 
    }; 

    System.out.println(foo.length); //2 
    System.out.println(foo[0].length); //3 
    System.out.println(foo[1].length); //4 
} 

列长度,每行是不同的。如果您通过固定大小的二维数组支持某些数据,则将getter提供给包装类中的固定值。

+0

关于您的答案的无关问题。称为“{...}”的技术/方法是什么?在对象定义之后。作为一名新开发人员,我越来越多地看到这一点。 – user432209 2010-10-22 19:27:06

+0

那么,我明白这一点:)。我只是觉得这个技术可能有一个特定的名字。 – user432209 2010-10-22 19:53:23

+0

不知道它的名字是什么 - 对象初始化?内联初始化?我们的一位Java大师将会知道 – 2010-10-22 19:59:24

4

Java允许您创建“粗糙数组”,其中每个“行”具有不同的长度。如果你知道你有一个方阵,你可以用你的代码修改,以防止像这样的空数组:

if (row > 0) col = test[0].length; 
3

有在语言级别不是一个干净的方式,因为不是所有的多维数组是矩形。有时锯齿状(不同的列长度)数组是必要的。

您可以轻松创建自己的类来提取所需的功能。

如果您不限于数组,那么也许某些不同的集合类也可以工作,如Multimap

10

二维数组不是矩形网格。或者更好的是,在Java中没有二维数组这样的东西。

import java.util.Arrays; 

public class Main { 
    public static void main(String args[]) { 

    int[][] test; 
    test = new int[5][];//'2D array' 
    for (int i=0;i<test.length;i++) 
     test[i] = new int[i]; 

    System.out.println(Arrays.deepToString(test)); 

    Object[] test2; 
    test2 = new Object[5];//array of objects 
    for (int i=0;i<test2.length;i++) 
     test2[i] = new int[i];//array is a object too 

    System.out.println(Arrays.deepToString(test2)); 
    } 
} 

输出

[[], [0], [0, 0], [0, 0, 0], [0, 0, 0, 0]] 
[[], [0], [0, 0], [0, 0, 0], [0, 0, 0, 0]] 

所述阵列testtest2是(或多或少)是相同的。

1

试试这个下面的程序二维数组中的java:

public class ArrayTwo2 { 
    public static void main(String[] args) throws IOException,NumberFormatException{ 
     BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); 
     int[][] a; 
     int sum=0; 
     a=new int[3][2]; 
     System.out.println("Enter array with 5 elements"); 
     for(int i=0;i<a.length;i++) 
     { 
      for(int j=0;j<a[0].length;j++) 
      { 
      a[i][j]=Integer.parseInt(br.readLine()); 
      } 
     } 
     for(int i=0;i<a.length;i++) 
     { 
      for(int j=0;j<a[0].length;j++) 
      { 
      System.out.print(a[i][j]+" "); 
      sum=sum+a[i][j]; 
      } 
     System.out.println(); 
     //System.out.println("Array Sum: "+sum); 
     sum=0; 
     } 
    } 
} 
0
public class Array_2D { 
int arr[][]; 
public Array_2D() { 
    Random r=new Random(10); 
    arr = new int[5][10]; 
    for(int i=0;i<5;i++) 
    { 
     for(int j=0;j<10;j++) 
     { 
      arr[i][j]=(int)r.nextInt(10); 
     } 
    } 
} 
    public void display() 
    { 
     for(int i=0;i<5;i++) 

     { 
      for(int j=0;j<10;j++) 
      { 
       System.out.print(arr[i][j]+" "); 
      } 
      System.out.println(""); 
     } 
    } 
    public static void main(String[] args) { 
    Array_2D s=new Array_2D(); 
    s.display(); 
    } 
    } 
+1

这与问题 – 2017-04-20 13:12:35

0

如果你有这样的数组:

String [][] example = {{{"Please!", "Thanks"}, {"Hello!", "Hey", "Hi!"}}, 
         {{"Why?", "Where?", "When?", "Who?"}, {"Yes!"}}}; 

你可以这样做:

example.length; 

= 2

example[0].length; 

= 2

example[1].length; 

= 2

example[0][1].length; 

= 3

example[1][0].length; 

= 4

+0

没有关系我之前看到过类似的答案,但我认为用例子比较容易理解! – Andy 2017-10-04 15:41:55