0
我想創建一個方法來列出一個ArrayList中的元素,但該方法只返回最新的對象的值:對象的一個ArrayList列出參數
對象:
//Constructor for guests at the hotel
public Guest (String name, String checkinDate, String checkoutDate, double depAmt)
{
gstName = name;
checkIn = checkinDate;
checkOut = checkoutDate;
deposit = depAmt;
}
ArrayList中當前對象:
//Fills the roster with three guests to start the program
public static void fillGuest()
{
Guest guest1 = new Guest("Mike P.", "2/13/16", "2/23/16", 360.00);
guestList.add(guest1);
Guest guest2 = new Guest("Randall M.", "4/10/16", "4/17/16", 120.00);
guestList.add(guest2);
Guest guest3 = new Guest("Bo-Diddly.", "4/20/16", "4/23/16", 600.00);
guestList.add(guest3);
}
方法列出ArrayList中的對象:
public static void returnGuests()
{
System.out.println("Currently checked in " + guestList.size() + " guests:");
for (int i = 0; i <= guestList.size() - 1; i++)
{
System.out.println(guestList.get(i));
}
}
輸出:
Currently checked in 3 guests:
Bo-Diddly. 4/20/16 4/23/16 600.0
Bo-Diddly. 4/20/16 4/23/16 600.0
Bo-Diddly. 4/20/16 4/23/16 600.0
Enter command:
如何顯示每個客戶對象的單個對象的參數?
你肯定還沒有定義的字段,靜態的?我們展示完整的客戶代碼源 –
你看着使用'foreach'循環呢?這將代替你現有的for循環。 – Brian
這是一個非常強烈的跡象表明你的'客人'類寫錯了。 –