Java 我需要找到数组中的第一个事件?

Java 我需要找到数组中的第一个事件?,java,arraylist,Java,Arraylist,获取日历中eventName等于给定名称的第一个事件 参数: name--要返回的ArrayList中的事件的事件名称 返回: 日历中名称等于给定名称的事件,如果不存在此类事件,则为null 代码: 我只是不明白这背后的逻辑是什么?我已经创建了日历数组,然后我的工作是从事件中获取名称,然后在日历中存储它 这听起来像是ArrayList保存了一个事件对象列表,其中有一个属性eventName。在getString name函数中,必须在该ArrayList中搜索与name参数匹配的eventNam

获取日历中eventName等于给定名称的第一个事件

参数:

name--要返回的ArrayList中的事件的事件名称

返回:

日历中名称等于给定名称的事件,如果不存在此类事件,则为null

代码:


我只是不明白这背后的逻辑是什么?我已经创建了日历数组,然后我的工作是从事件中获取名称,然后在日历中存储它

这听起来像是ArrayList保存了一个事件对象列表,其中有一个属性eventName。在getString name函数中,必须在该ArrayList中搜索与name参数匹配的eventName,然后返回该事件

该搜索可能类似于:

public Event get(String name) {
   for(Event calEvent: this.calendar) {
      if(calEvent.eventName.equals(name))
         return calEvent;
   }
}
解决方案:

/*
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;
}

由于某些原因,在使用if语句时找不到eventName,在calEvent.eventName下面有一条红线,我将其命名为first。我相信这是因为我的事件类中没有var eventName,我的名字var是private@elliot,因为它是private的,这是有道理的。事件类中是否有返回私有变量的函数?如果没有公共字符串getEvent{return privateVarHere;}之类的内容,您可能需要编写它。我不知道事件类是什么样子。
/*
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;
}