Curriculum
Course: Learn Java Programming
Login

Curriculum

Learn Java Programming

Text lesson

Returning 2D Array of Objects from a Method in Java

In this lesson, you will learn

  • Returning a 2D Array of Objects from a Method in Java
  • Example

 

Returning a 2D Array of Objects from a Method in Java

  • Returning a 2D array of objects from a method in Java can be very useful in applications that deal with tabular data, board games, or any scenario that requires a grid layout.
  • To demonstrate this with a real-life example, let’s consider a simple application for managing seating arrangements in a cinema hall.
  • In this example, we’ll represent each seat as an object with properties for its row and column, and whether it is booked.
  • We’ll then create a method that initializes and returns a 2D array of seat objects, representing the layout of the cinema hall.

 

Example 1: Cinema Hall Seating Arrangement

package ch7.l7;

class Seat {
    int row;
    int column;
    boolean isBooked;

    // Constructor to initialize the seat
    public Seat(int r, int c) {
        row = r;
        column = c;
        isBooked = false; // All seats are initially not booked
    }

    // Method to book the seat
    public void bookSeat() {
        isBooked = true;
    }

    // Method to check if the seat is booked
    public boolean isBooked() {
        return isBooked;
    }

    @Override
    public String toString() {
        return "Seat: " + row + "," + column + " - " 
        		+(isBooked ? "Booked" : "Available");
    }
}

class CinemaHall {
    // Method to generate and return a 2D array of Seat objects
    public static Seat[][] initializeSeating(int rows, int cols) {
        Seat[][] seats = new Seat[rows][cols];

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                seats[i][j] = new Seat(i + 1, j + 1); 
                // Creating seat objects with 1-based indexing 
                // for rows and cols
            }
        }

        return seats;
    }
}

public class CinemaHallMain {
    public static void main(String[] args) {
        // Initialize the seating arrangement for a cinema 
    	// hall with 5 rows and 8 columns
        Seat[][] cinemaSeats = CinemaHall.initializeSeating(5, 8);

        // Example: Book some seats
        cinemaSeats[2][3].bookSeat(); // Booking seat at row 3, column 4
        cinemaSeats[4][7].bookSeat(); // Booking seat at row 5, column 8

        // Print the seating arrangement
        for (Seat[] row : cinemaSeats) {
            for (Seat seat : row) {
                System.out.println(seat);
            }
        }
    }
}

Output

Seat: 1,1 - Available
Seat: 1,2 - Available
Seat: 1,3 - Available
Seat: 1,4 - Available
Seat: 1,5 - Available
Seat: 1,6 - Available
Seat: 1,7 - Available
Seat: 1,8 - Available
Seat: 2,1 - Available
Seat: 2,2 - Available
Seat: 2,3 - Available
Seat: 2,4 - Available
Seat: 2,5 - Available
Seat: 2,6 - Available
Seat: 2,7 - Available
Seat: 2,8 - Available
Seat: 3,1 - Available
Seat: 3,2 - Available
Seat: 3,3 - Available
Seat: 3,4 - Booked
Seat: 3,5 - Available
Seat: 3,6 - Available
Seat: 3,7 - Available
Seat: 3,8 - Available
Seat: 4,1 - Available
Seat: 4,2 - Available
Seat: 4,3 - Available
Seat: 4,4 - Available
Seat: 4,5 - Available
Seat: 4,6 - Available
Seat: 4,7 - Available
Seat: 4,8 - Available
Seat: 5,1 - Available
Seat: 5,2 - Available
Seat: 5,3 - Available
Seat: 5,4 - Available
Seat: 5,5 - Available
Seat: 5,6 - Available
Seat: 5,7 - Available
Seat: 5,8 - Booked

 

 

Understanding a toString Method

 

Example 1: Without toString() Method

package ch7.l7;

class Student{  
    int rollno;  
    String name, city;  
     
    Student(int roll, String n, String c){  
	    rollno=roll;  
	    name=n;  
	    city=c;  
    }  
    
    public static void main(String args[]){
    	Student s1=new Student(1001,"Somya","Delhi");  
    	Student s2=new Student(1002,"James","USA");  
        
    	System.out.println(s1);//compiler writes here s1.toString()  
    	System.out.println(s2);//compiler writes here s2.toString()  
    }
}  

Output

ch7.l7.Student@34a245ab
ch7.l7.Student@7cc355be

Explanation

In the above example, the compiler prints the hashcode of the corresponding objects instead of printing its values. Java Compiler calls the toString() method internally to print the same.

 

Example 2: Using toString() Method

package ch7.l7;

class Student{  
    int rollno;  
    String name, city;  
     
    Student(int roll, String n, String c){  
	    rollno=roll;  
	    name=n;  
	    city=c;  
    }
    @Override
    public String toString() {
		return rollno+","+name+","+city;
    }
    
    public static void main(String args[]){
    	Student s1=new Student(1001,"Somya","Delhi");  
    	Student s2=new Student(1002,"James","USA");  
        
    	System.out.println(s1);//compiler writes here s1.toString()  
    	System.out.println(s2);//compiler writes here s2.toString()  
    }
}  

Output

1001,Somya,Delhi
1002,James,USA

Explanation

In the above example, the Java compiler internally calls the toString() method, overriding this method will return the specified values of the s1 and s2 objects of the Student class.

 

 


End of the lesson….enjoy learning

 

 

Student Ratings and Reviews

 

 

 

There are no reviews yet. Be the first one to write one.

 

 

Submit a Review