我正在尝试将预订添加到自定义预订列表。
这是预订舱位
public class Booking {
String forename = null;
String surname = null;
int numberOfSeats = 0;
public Booking() {
this.forename = forename;
this.surname = surname;
this.numberOfSeats = numberOfSeats;
}
在另一个类中,我声明了一个列表,有一个方法将预订添加到此列表:
public class TheatreShow {
List<Booking> booking = new ArrayList<Booking>();
public void addBooking(String forename, String surname, int numberOfSeats) {
booking.add(numberOfSeats, surname, numberOfSeats);
}
我收到的错误是它不适用于参数。
The method add(int, Booking) in the type List<Booking> is not applicable for the arguments (int, String, int)
最佳答案
改变
booking.add(numberOfSeats, surname, numberOfSeats);
至
booking.add(new Booking(forename, surname, numberOfSeats));
您的列表是预订
列表,因此它仅存储预订
类型的项目。您需要使用通过参数获取的 forename
、surname
和 numberOfSeats
创建一个新的 Boooking
,然后再添加到列表。
关于java - 添加到自定义列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58960633/