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

huangapple go评论114阅读模式
英文:

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&lt;Event&gt; calendar;

public TRAPSCalender() {
    calendar = new ArrayList&lt;&gt;();
}

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

huangapple
  • 本文由 发表于 2020年4月7日 05:52:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/61069531.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定