2010-10-22 53 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