Java Linked List How to create a node that holds a string and an int?

落爺英雄遲暮 提交于 2020-01-04 06:23:11

问题


I have been at this literally all day. I can create linked lists no problem and display/delete the data in them. My problem is though that I am not sure how to create a linked list of flights with each node including a reference to a linked list of passengers? This is an assignment in my advanced Algorithms class. I am drawing a blank here?


回答1:


Create an object that holds a Passenger:

public class Passenger
{
    private String name;
    private int id;
}

Then give Flight a List of Passengers:

public class Flight
{
    private List<Passenger> passengers;
}

Now you can have a List of Flights:

public class Schedule
{
    private List<Flight> flights;
}

You needs lots more code in each. Be sure to override equals and hashCode for Passenger and Flight to make sure that they work properly.




回答2:


Well, can't you just create a Flight class and a Passenger class?

class Flight {
   private LinkedList<Passenger> passengers;
   ...
}

class Passenger {
  ... 
}

LinkedList<Flight> flights = ...


来源:https://stackoverflow.com/questions/6246997/java-linked-list-how-to-create-a-node-that-holds-a-string-and-an-int

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!