Home > OS >  How can i put an Arraylist into Arraylist
How can i put an Arraylist into Arraylist

Time:06-12

i have an exercise which i must write a programm in which i manage the timetable of trains.I write all the programm put i can not write the main class, because i must make an Arraylist with 3 passengers and i dont know what i must put in the blank

Code:

public class Route {
private int id;
private int aeroplane;
private String departure;
private String arrival;
private ArrayList<Ticket> Tickets = new ArrayList<>() ;
public Route(){
    id = 0 ;
    aeroplane = 0  ;
    departure = " ";
    arrival = " ";
    Tickets = new ArrayList<>();
    
}

public Route(int ID, int aerop, String depar,String arriv,ArrayList<Ticket> tick ){
    id=ID;
    aeroplane=aerop;
    departure=depar;
    arrival=arriv;
    Tickets=tick;
}



public static void main(String[] args) {
   ArrayList <Train> train=new ArrayList<>();
   Route d1= new Route(0051,50,"Greece","Italy",);// what i have to write in the last blank?
   
}}

CodePudding user response:

In your main method ( it's not a class ) :

List<Ticket> tickets = new ArrayList<>();
tickets.add( new Ticket());
Route d1 = new Route(0051,50,"Greece","Italy",tickets);

and as it was mentined before you need to define a constructor :

public Route(int id, int aeroplane, String departure, String arrival, List<Ticket> tickets ){
    this.id = id;
    this.aeroplane = aeroplane;
    this.departure = departure;
    this.arrival   = arrival;
    this.Tickets   = tickets;

}

CodePudding user response:

I suppose you have those three classes:

  • Train
  • Ticket - which is valid for multiple trains
  • Route - which could contain multiple Tickets

Then you should model your Route like that:

public class Route {
    private final int id;
    private final int aeroplane;
    private final String departure;
    private final String arrival;
    private final List<Ticket> tickets;

    public Route(int id, int aeroplane, String departure, String arrival, List<Ticket> tickets) {
        this.id = id;
        this.aeroplane = aeroplane;
        this.departure = departure;
        this.arrival = arrival;
        this.tickets = tickets;
    }
}

You route class does not have, aber should not have a list of lists of trains. It is perfectly fine, that you have it has a list of tickets.

To the question of how to add the list of Trains to the Route instantiation, you should create a ticket first, or multiple if you like.

public static void main(String[] args) {
    // create trains
    Train train1 = new Train(1202, "Piraeus", "Athens");
    Train train2 = new Train(1302, "Athens", "Thessaloniki");
    Train train3 = new Train(1502, "Thessaloniki", "Rome");

    // create ticket(s)
    Ticket ticket = new Ticket(95, List.of(train1, train2, train3));

    // create route and pass tickets
    Route myRoute = new Route(0051, 50, "Greece", "Italy", List.of(ticket));
}

To create the lists here I used the factory method java.util.List.of (available since Java 9). Your route class is also a good candidate for a record (since Java 18). As an record it would look like:

public record RouteRecord(int id, int aeroplane, String departure, String arrival, List<Ticket> tickets) {}

Also, think about the concept of immutability.

  • Related