检查ArrayList中是否已存在对象

时间:2017-01-07 17:08:23

标签: java arraylist

我对Java很新,我正在尝试创建一个简单的程序,在其中向ArrayList添加事件。现在我正在尝试创建一个函数来检查用户输入的事件是否已存在于Arraylist中。如果是,我希望程序打印一条消息,退出方法并返回开始菜单。有什么建议?到目前为止,这是我的代码:

    public void addEvent() {

    System.out.println("Event name: ");
    String eventName = norm();

    for (int i = 0; i < allEvents.size(); i++) {

        if (allEvents.get(i).getEventName().equalsIgnoreCase(eventName)) 
        System.out.println(eventName + " has already been added! ");

        **// Break out of method and return to menu**
    }

    System.out.println("Attempts allowed: ");
    int noOfAttempts = readInt();
    while (noOfAttempts < 1) {
        System.out.println("To low, must allow at least 1 attempt. Try again: ");
        noOfAttempts = readInt();
    }
    allEvents.add(new Event(eventName, noOfAttempts));
    System.out.println(eventName + " added \n");

    }

0 个答案:

没有答案