-1

I have a class called Seat as follows:

public class Seat {
    private String seatType; // E for economy, B for business class
    private boolean Available; // false:booked, true:available

    public Seat (String seatType, boolean Available) {
        this.seatType = seatType;
        this.Available = Available;
    }
    public boolean getAvailability() {
        return Available;
    }
    public String getSeatType() {
        return seatType;
    }
}

I would like to create an array called Seats with 10 elements where each of the elements is of type Seat as in the class above. I would like to initialize it with an assignment statement where each element is false for the seatType and 'E' as the seat type.

Can someone provide me with the assignment statement that will accomplish this?

6
  • 3
    What have you tried so far? Please provide us with your attempt to solve the problem. We are not here to do the work for you. Commented Mar 1, 2018 at 10:18
  • you'll need more than one statement. First, you'll need to create the array, then you'll need to initialize every element. Commented Mar 1, 2018 at 10:19
  • 1
    you also might want to provide setters, and read up on naming conventions Commented Mar 1, 2018 at 10:20
  • 1
    @Stultuske You can do it in one with a generated limited stream and an array collector. Commented Mar 1, 2018 at 10:21
  • 1
    1) How do you create an array? 2) How do you create an instance of Seat with the required properties? 3) How do you assign a value to an array element? Put them together with a loop. Commented Mar 1, 2018 at 10:23

3 Answers 3

0

First of all, you should create your 'initial' Seat:

public class Seat
{
  public static final DEFAULT_SEAT = new Seat("E", false);
  ...
}

Then, create your array with these seats:

List<Seat> seats;
int        counter;
seats = new ArrayList<Seat>();
for (counter = 0; counter < 10; counter++)
  seats.add(Seat.DEFAULT_SEAT);

Which, of course, can also be created like this:

Seat[] seats;
int    counter;
seats = new Seat[10];
for (counter = 0; counter < seats.length; counter++)
  seats[counter] = Seat.DEFAULT_SEAT;

If you want separate instances for each element in the array, you could also simply do this (without defining the Seat.DEFAULT_SEAT:

seats[counter] = new Seat("E", false);
Sign up to request clarification or add additional context in comments.

5 Comments

It's unlikely that OP wants the same instance in each element in the array. Also, it says array, not ArrayList.
It seams the Seat class is not mutable. No problem therefore to have the same instance in each element. Updated the answer as far as the second comment is concerned.
In case separate instances are required, the Seat class could implement Cloneable and the assignment would become Seat.DEFAULT_SEAT.clone()
there is no need to involve cloning or a default instance. Just create separate instances in the loop.
Updated the answer again.
0

Here's how you create an array and fill it with instances:

// array size
int n = 10;

// create array
Seat[] list = new Seat[n];

// fill
for (int i = 0; i < n; i++) {
    list[i] = new Seat("E",false);
}

Comments

-2

Take a look at Arrays.Fill(Object[] a, Object val) for example

2 Comments

It's unlikely that OP wants the same instance in each element in the array.
Yeah you're probably right

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.