KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I need to get the length of a 2D array for both the row and column. I’ve successfully done 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); } } This prints out 5, 10 as expected. Now take a look at this line: int col = test[0].length; Notice that I actually have to reference a particular row, in order to get the column length. To me, this seems incredibly ugly. Additionally, if the array was defined as: test = new int[0][10]; Then the code would fail when trying to get the length. Is there a different (more intelligent) way to do this?
Tags (comma-separated)
Save Edits
Cancel