• Post Reply Bookmark Topic Watch Topic
  • New Topic

How would I display a 2d array in a method if I am not allowed to pass in the row and column?  RSS feed

 
Jackson Blackwale
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I get user input from the user for the row and column and I create an array based on that and fill it with random numbers. How would I display the array in a method if all I can pass to that method is the 2d array itself?





If I were allowed to pass in the row and column I could just use a nested for loop but if we are only allowed to pass in the 2d array itself I am not really sure what to do :S

 
Jackson Blackwale
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I got it. So to find number of rows it is i<array.length

To find number of columns it is i<array[i].length if i is the row
 
Abhay Agarwal
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to Java Ranch and world of Java

You are correct.

Happy Learning

~ abhay
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are not creating a 2-d array, as java has no such construct.

What you are creating is a 1-d array that happens to hold a bunch of arrays.

Note that each of those arrays can be of different lengths, so the number of columns does not have to be the same.
 
Phil English
Ranch Hand
Posts: 62
MySQL Database Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does each column/row correspond to? When I came across this problem the issue that Fred highlighted made me feel that the array-of-arrays seemed clumsy for my purpose. It transpired that each row was in fact a Point with an X state and a Y state corresponding to the columns. So rather than an array of 2-element arrays I created an array of Point objects. Could be overkill sometimes but makes things much cleaner.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tend to use an egg analogy. the eggs are what you are trying to store - ints, floats, Strings, MyCustomObject...whatever.

An array is like a carton. It has a fixed size. It may hold up to 12 eggs, or 6, or 64. some of the spots may be empty. The filled ones do not have to be contiguous.

in Java, a 2-d array is like a crate that holds cartons. It can hold many cartons, and the cartons do not have to all be the same size. You may have a 12 egg carton next to a 6 egg carton next to a 47 egg carton. when you unpack the crate, you pull out one carton at a time, and can look and see what size each one is as you go.

a 3D array would be like a shipping palate with crates on it. Each crate may be a different size.

a 4D array would be like a truck full of palates...

a 5-d array would be a company's fleet of trucks...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!