我是新來的java,我試圖編寫我的第一個「更大」的程序,有幾個類。我在類「CompetitionProgram」中創建了一個ArrayList,並聲明它是私有的。將對象從一個類添加到另一個類的ArrayList中
public class CompetitionProgram {
private ArrayList<ListOfEvents> eventList = new ArrayList<ListOfEvents>();
在方法 「的addEvent」,我創建類事件的對象:
private void addEvent() {
System.out.print("Event name: ");
String eventName = checkInput();
System.out.print("Attempts allowed: ");
int noOfAttempts = readInt();
Event ev = new Event(eventName, noOfAttempts);
eventList.add(ev);
事件類:
public class Event {
private String eventName;
private int noOfAttempts;
public Event(String eventName, int noOfAttepmts) {
this.eventName = eventName;
this.noOfAttempts = noOfAttepmts;
}
public String getEventName() {
return eventName;
}
public int getNoOfAttempts() {
return noOfAttempts;
}
ListOfEvents-類(它沒有完成,我知道我不能返回「事件」等):
public class ListOfEvents {
public ListOfEvents() {
}
public Event addEvent(eventName, noOfAttempts) {
// code -add event to the list
// code
return event;
}
public Event findEvent(eventName) {
// code -check if event already is in the list
// code
return event;
}
public boolean isEvent(eventName) {
//code -check if input is empty
//code
return true or false;
}
我想將Event對象添加到頂部聲明的ArrayList,但是我不能,因爲ArrayList是類「ListOfEvents」的列表。所以,我的問題是將Event對象添加到類ListOfEvents的ArrayList中。
我需要這兩個類(事件& ListOfEvents)在我的程序中,這是需求之一。 事件 - 僅表示事件本身,如事件名稱和嘗試次數(其體育事件)。而ListOfEvents - 代表名單本身包含用於添加事件,刪除事件,並檢查該事件是否已經在列表等
任何人誰有任何想法如何解決這個問題的方法呢?
Post ListOfEvents也是如此。名稱在這裏有誤導性。 ListOfEvents應該可能包含一個列表,而不是添加到列表中的類。 – dev8080
'ListOfEvents list = new ListOfEvents(); list.add(EV); eventList.add(列表);' – 4castle