'使用for循环来比较ArrayList的每个元素是否等于用户响应

时间:2017-03-22 10:49:57

标签: java arraylist

我已经想出了如何使用不同的方法来做到这一点,但显然我必须使用for循环,我觉得我使用了错误的语法或其他东西,但我已经用Google搜索并搜索了近一个小时现在所以我不知道如何解决这个看似简单的问题。

以下是我认为代码的相关部分:

public static void deleteEntry()
    {
        response =  JOptionPane.showInputDialog("Please enter a bird name to REMOVE it from the list");

        for(int i=0;i<birdList.size();i++)
        {
            if(birdList(i).equals(response)) **//this line is giving me an error. birdList has a red line under it in eclipse.**
            {
                birdList.remove(response);
                JOptionPane.showMessageDialog(null, response + " removed!");
            }
            else if(i == birdList.size())
            {
                JOptionPane.showMessageDialog(null, "The bird you tried to remove was not on the list");
            }
        }

        /* How I was originally going to do it before I re-read the requirements.
        /*if(birdList.contains(response))
        {
            birdList.remove(response);
            JOptionPane.showMessageDialog(null, response + " removed!");
        }
        else
        {
        JOptionPane.showMessageDialog(null, "The bird you tried to remove was not on the list");
        }*/

以下是所有代码:     import java.util.ArrayList;     import javax.swing.JOptionPane;

public class BirdArrayList {

    static ArrayList<String> birdList = new ArrayList<String>(5); 
    static String response;

    public static void main(String[]args)
    {

        birdList.add("Crow");  //adding fice initial birds to the birdList ArrayList
        birdList.add("Magpie");
        birdList.add("Dove");
        birdList.add("Finch");
        birdList.add("Kookaburra");


        while (response != "5")
        {
            response =  JOptionPane.showInputDialog("Please enter the number for what you wish to do \n"
                    + " 1 \t ADD a bird to the list \n"
                    + " 2 \t REMOVE a bird from the list \n"
                    + " 3 \t SEARCH for a bird in the list \n"
                    + " 4 \t DISPLAY the list of birds \n"
                    + " 5 \t QUIT the program"); // Main screen of the program, formatted nicely

            if(response.equals("1")) // Add a bird
            {
                addEntry();
            }

            if(response.equals("2"))//Remove a bird from the list
            {
                deleteEntry();
            }

            if(response.equals("3"))//Search for a bird
            {
                //searchEntry();
            }


            if(response.equals("4"))  // Displays the array
            {
                displayArray();
            }
            if(response.equals("5")) // Quit the program
            {
                System.exit(0);
            }
        }

    }

    public static void addEntry()
    {
        response =  JOptionPane.showInputDialog("Please enter a bird name to ADD it to the list"); 
        birdList.add(response); // adds the response to the birdList in the next Array position
         JOptionPane.showMessageDialog(null, response + " added!"); // Shows bird name added
         displayArray();
    }

    public static void deleteEntry()
    {
        response =  JOptionPane.showInputDialog("Please enter a bird name to REMOVE it from the list");

        for(int i=0;i<birdList.size();i++)
        {
            if(birdList(i).equals(response))
            {
                birdList.remove(response);
                JOptionPane.showMessageDialog(null, response + " removed!");
            }
            else if(i == birdList.size())
            {
                JOptionPane.showMessageDialog(null, "The bird you tried to remove was not on the list");
            }
        }

        /* How I was originally going to do it before I re-read the requirements.
        /*if(birdList.contains(response))
        {
            birdList.remove(response);
            JOptionPane.showMessageDialog(null, response + " removed!");
        }
        else
        {
        JOptionPane.showMessageDialog(null, "The bird you tried to remove was not on the list");
        }*/
    }
    public static void displayArray()
    {
        String displayList = "";
        int tempCounter = 0;
        for(int i=0;i<birdList.size();i++) //Display the ArrayList 
        {
            String tempBird = birdList.get(i); //Makes tempBird more workable in the displayList formula 2 lines down
            tempCounter++; // shows a nuber before the bird in the list
            displayList += tempCounter + ".   " + tempBird + "\n";   //formatting nicely    
        }
            JOptionPane.showMessageDialog(null, displayList); //Displays the list

    }
}

3 个答案:

答案 0 :(得分:1)

我最终在reddit上找到了答案,应该是

if(birdList.get(i).equals(response))

而不是

if(birdList(i).equals(response))

感谢所有其他答案。

答案 1 :(得分:0)

 while (response != "5")

你不应该将String与==或!=进行比较,因为它比较两个对象是否相同而你想比较它们的相等性。
请使用equals(Object o)方法。

此外,如果"5"NullPointerException,则应先将response放在比较之前,以避免null

   while (!"5".equals(response))

最后,你没有处理null案件。

答案 2 :(得分:0)

如果您使用的是Java 8,则可以使用List :: removeIf方法。

birdList.removeIf(bird -> bird.equals(response));