Home > Enterprise >  Is there a way to create a variable in JAVA that stores a arraylist and each element in the arraylis
Is there a way to create a variable in JAVA that stores a arraylist and each element in the arraylis

Time:07-11

Image of variable hierarchy (Please Check)

I want this specific variable to be a list of usernames, and each of these users will have some bookings... I tried adding an arraylist within an arraylist but that doesnt allow the user to name the usernames, they have to be predefined, please give me a method to do this.

CodePudding user response:

You can store any kind of object in an ArrayList.

You could define your own class Booking that contains information about a booking:

public class Booking {
    // ... whatever information is necessary for a booking
}

And then define a class User that contains information about a user, including a list of bookings:

public class User {
    private String name;
    private List<Booking> bookings = new ArrayList<>();

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Booking> getBookings() {
        return bookings;
    }

    // ... other methods as necessary
}

And then you can make an ArrayList of User objects, where each User object contains a list of Booking objects:

List<User> users = new ArrayList<>();
  • Related