Getting array length of a 2D array in Java

I need to get the length of a 2D array for both a row and a column. Ive successfully accomplished this using the following code:

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); } } 

Will print 5, 10 as expected.

Now take a look at this line:

  int col = test[0].length; 

Note that I really need to refer to a specific row in order to get the length of the column. It seems incredibly ugly to me. Also, if the array was defined as:

 test = new int[0][10]; 

Then the code will fail when trying to get the length. Is there any other (more intelligent) way to do this?

+98
java arrays multidimensional-array
Oct 22 '10 at 19:15
source share
10 answers

Consider

 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 } 

Column lengths vary for each row. If you support some data using a 2D array with a fixed size, then provide the recipients with fixed values ​​in the wrapper class.

+135
Oct 22 '10 at 19:21
source share

A 2D array is not a rectangular grid. Or maybe better, in Java there is no such thing as a 2D array.

 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)); } } 

Outputs

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

The arrays test and test2 are (more or less) the same.

+11
Oct 22 '10 at 19:35
source share

It was really hard to remember.

  int numberOfColumns = arr.length; int numberOfRows = arr[0].length; 

Let's understand why this is so, and how we can find out when we are asked a problem with arrays. From the code below you can see that rows = 4 and columns = 3:

  int[][] arr = { {1, 1, 1, 1}, {2, 2, 2, 2}, {3, 3, 3, 3} }; 

arr has several arrays, and these arrays can be arranged vertically to get the number of columns. To get the number of rows, we need to access the first array and consider its length. In this case, we get access to [1, 1, 1, 1] and, thus, the number of rows = 4. When you are asked a problem, when you do not see the array, you can visualize the array as a rectangle with n X m dimensions and we conclude that we can get the number of rows by accessing the first array, and then its length. Another ( arr.length ) for columns.

+5
Jan 23 '19 at 16:00
source share

Java allows you to create β€œdangling arrays,” where each β€œrow” has a different length. If you know that you have a square array, you can use your code to protect against an empty array as follows:

 if (row > 0) col = test[0].length; 
+4
Oct 22 '10 at 19:21
source share

There is no cleaner way at the language level, because not all multidimensional arrays are rectangular. Sometimes arrays with jagged (different columns) are required.

You can easily create your own class for the abstract functionality that you need.

If you are not limited to arrays, perhaps some of the collection classes will work like Multimap .

+3
Oct 22 '10 at 19:23
source share

If you have this array:

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

You can do it:

 example.length; 

= 2

 example[0].length; 

= 2

 example[1].length; 

= 2

 example[0][1].length; 

= 3

 example[1][0].length; 

= 4

+3
03 Oct '17 at 15:15
source share

Try the following program for a 2d array in 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; } } } 
+1
Jan 11 '17 at 5:20
source share
 import java.util.Arrays; public class Main { public static void main(String[] args) { double[][] test = { {100}, {200}, {300}, {400}, {500}, {600}, {700}, {800}, {900}, {1000}}; int [][] removeRow = { {0}, {1}, {3}, {4}, }; double[][] newTest = new double[test.length - removeRow.length][test[0].length]; for (int i = 0, j = 0, k = 0; i < test.length; i++) { if (j < removeRow.length) { if (i == removeRow[j][0]) { j++; continue; } } newTest[k][0] = test[i][0]; k++; } System.out.println(Arrays.deepToString(newTest)); } } 
0
Nov 11 '18 at 16:54
source share

.length = row count / column length

[0] .length = number of columns / row length

0
May 16 '19 at 15:18
source share
 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(); } } 
-one
Apr 20 '17 at 13:00
source share



All Articles