英文:
I need to find the first event in an array?
问题
Fetch the first Event in the calendar whose eventName is equal to the given name
Parameters:
name - the Event name of the Event within the ArrayList to be returned
Returns:
the Event in the calendar whose name is equal to the given name, or null if no such Event exists
Code:
**This is just for reference, the get is what im stuck on**
ArrayList<Event> calendar;
public TRAPSCalender() {
calendar = new ArrayList<>();
}
public Event get(String name) {
if(name != null) {
return calendar;
}
}
英文:
Fetch the first Event in the calendar whose eventName is equal to the given name
Parameters:
name - - the Event name of the Event within the ArrayList to be returned
Returns:
the Event in the calendar whose name is equal to the given name, or null if no such Event exists
Code:
**This is just for reference, the get is what im stuck on**
ArrayList<Event> calendar;
public TRAPSCalender() {
calendar = new ArrayList<>();
}
public Event get(String name) {
if(name != null) {
return calendar;
}
}
I just can't seem to figure out what the logic behind this is? I have created the calendar array, then my job is to get the name from the event and then in the calendar store it.
答案1
得分: 1
听起来像是ArrayList保存了一系列的Event
对象,这些对象有一个名为eventName
的属性。在你的get(String name)
函数中,你需要在这个ArrayList中搜索匹配名称参数的eventName
,然后返回那个Event
对象。
搜索的过程可能类似于:
public Event get(String name) {
for(Event calEvent: this.calendar) {
if(calEvent.eventName.equals(name))
return calEvent;
}
}
英文:
What it sounds like is that the ArrayList holds a list of Event
objects, which have an attribute eventName
. In your get(String name)
function you have to search through that ArrayList for an eventName
that matches the name parameter, then return that Event
.
That searching might look something like:
public Event get(String name) {
for(Event calEvent: this.calendar) {
if(calEvent.eventName.equals(name))
return calEvent;
}
}
答案2
得分: 0
解决方案:
/*
获取日历中第一个事件,其事件名称与给定名称相等
参数:
name - 要返回ArrayList中的事件的事件名称
返回:
日历中名称与给定名称相等的事件,如果不存在这样的事件,则返回null
*/
public Event get(String name) {
for (Event firstEvt : this.calendar) {
if (firstEvt.getEventName().equals(name)) { // 使用访问器getEventName来访问私有变量
return firstEvt;
}
}
return null;
}
英文:
Solution:
/*
Fetch the first Event in the calendar whose eventName is equal to the given name
Parameters:
name - - the Event name of the Event within the ArrayList to be returned
Returns:
the Event in the calendar whose name is equal to the given name, or null if no such Event exists
*/
public Event get(String name) {
for(Event firstEvt: this.calendar) {
if(firstEvt.getEventName().equals(name)) { // Used accessor getEventName to access the private var
return firstEvt;
}
}
return null;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论