• Post Reply Bookmark Topic Watch Topic
  • New Topic

How do I get the values of a 2d array returned and then accessed in another class in Java?  RSS feed

 
Caiz Austin
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to be able to use the "row" and "col" variable to book a seat, however after I return the values and then go back to the other class where I called the method I can't use the values of the returned "seat[row][col]".

For example, if the returned value was seats[0][4], I want to be able to use the indices of the seats array: row:0 and col:4 in the first set of code which I can book a seat from these values. My problem is currently when it goes back to the first method, it is booking the seat: seats[0][0] instead of e.g. seats[0][4](should have been returned) which would be the correct seat to book. How can I access the the returned values? SeatT has the type of seat which is an Enum either Wall, middle, or aisle

Where I am calling the method to return a free seat from:


Where I am finding which is an available seat:



I have tried saving the returned value of the called method into another variable e.g. Seats[][] seat = findAvailableSeats(), but this doesn't work.

Thanks in advance

 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Caiz,

welcome to the Ranch!

In your two code snippets you have two nested loops. In the second snippet a free Seat is returned. What is unknown is what row and what column does that Seat have?
One possibility is to add the row and column of a Seat as variables to the Seat, whenever a Seat is constructed. So, when an available Seat is returned, the calling method can then simply ask that Seat for its row and column, and update the seats[][] accordingly.
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again!

To generalize what Piet posted, any time you need to return data that is more than one type, you can construct a static inner class to use as a return type for the method.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch again.

Don't write long lines like line 7.
Never use == false or == true; both are poor style and error‑prone because you might write = instead of ==.
Never
if (b == true) ...
Always
if (b) ...
Never
if (b == false) ...
Always
if (!b) ...

You have been given the misconception that there is such a thing as a 2D array. 2D arrays exist in some languages but not in Java®, even if people tell you there are 2D arrays. What you have is an array of arrays, which is better than a 2D array.
You must also have some bad design if you can write journey.floor.getLastRow(journey). That violates encapsulation by having non‑private fields, and there is something wrong with having to iterate an array to find vacant seats. Also, why are you using journey at the beginning of that expression, as well as an argument to the method; that looks very strange. The object which models the theatre shou‍ld go searching for vacant seats itself. PS is quite right that you shou‍ld create a Seat class which can encapsulate its row and number; that is a much more object‑oriented solution than what you have shown. I would suggest that you shou‍ld write a method returning a boolean which tells you whether there are any seats at all, alongside a separate method to find a seat. Do you really want a loop to identify vacant seats? If you use the loop, your theatre will gradually fill up from one corner and one row will remain vacant until the place is almost completely booked.
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
seats[row][col].getFirstClass() == false
seats[row][col].booked == false

1. For the expressions above, well, for the methods which return boolean values, there is a convention which you could employ. Change getFirstClass() to isFirstClass() and eliminate == false as Campbell already said.

2. booked == false. Wait! Why booked is accessed directly? This member variable should be given private access modifier and accessed only via its getter method !isBooked() (without false), that seemed to be mentioned already too.

3. Triple check if to return null from a method is a best choice in those exceptional cases. Consider using Optional class; ref 1, ref 2.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!