【问题标题】:Search for value and keep going寻找价值并继续前进
【发布时间】:2020-07-08 10:44:41
【问题描述】:

我有一个包含项目列表的程序。

然后我搜索一个字符串并尝试在所述列表中找到它。

我想要发生的事情是:它找到第一个匹配项,然后将其打印出来或保存在某处,以便我以后可以使用它(例如,通过突出显示用户选择)。然后,一旦我再次搜索相同的字符串,它应该返回第二个匹配项,然后是第三个匹配项,等等。当它没有找到更多匹配项时,它应该返回开始并重复。

这是我所拥有的一个粗略的例子:

public static void main(String[] args) {

        ArrayList<String> exampleList = new ArrayList<String>();

        exampleList.add("string1");
        exampleList.add("john");
        exampleList.add("string2");
        exampleList.add("arnold");
        exampleList.add("string3");

        String inputExample = "str"

        for (String s : exampleList) {

            if (s.contains(inputExample)) {

                System.out.println(s);
                break;
            }

        }

    }

我在if 中添加了break,所以我只能得到第一个结果。如果我删除它,我将得到所有结果。

在程序中,我有与单击按钮相关联的类似代码,如果列表包含用户搜索的内容,则突出显示用户选择,这只是一个示例。

在我的程序中发生的情况是,只有第一个结果会被突出显示(因为我跳出了循环)或只有最后一个结果会被突出显示(因为我没有跳出循环)。

我怎样才能使搜索从中断的地方继续,正式忽略它已经获得的结果?

【问题讨论】:

    标签: java search arraylist find


    【解决方案1】:

    正如@Alex 在他的回答中指出的那样,您应该使用iterator 并将您的结果保存在私人ArrayList 中,以便进一步使用这些搜索结果。

    如果您不熟悉iterator,请使用this

    我编写了一个简单的类HighlightingUserInput,它的行为符合您的条件。它给出第一个搜索结果并将其打印并保存在ArrayList,如果你没有退出程序(或者你可以添加一个搜索按钮,如果用户再次按下搜索)然后给出第二个搜索结果,等等。

    请随时询问与以下程序相关的任何问题。

    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.Scanner;
    
    public class HighlightingUserInput{
    
       private static ArrayList<String> resultsString=new ArrayList<>();
    
       public static void main(String [] args) {
    
          ArrayList<String > allStrings=new ArrayList<>();
    
          allStrings.add("string1");
          allStrings.add("john");
          allStrings.add("string2");
          allStrings.add("arnold");
          allStrings.add("string3");
    
          Scanner scanner=new Scanner(System.in);
    
          CharSequence userIput=scanner.nextLine();
    
          Iterator iterator = allStrings.iterator();
    
          while(!scanner.nextLine().equals("exit")) {
    
             inner:
             while (iterator.hasNext()) {
                String s = (String) iterator.next();
                if (s.contains(userIput)) {
                    System.out.println(s);
                    HighlightingUserInput.resultsString.add(s);
                    break inner;
                }
    
             }
    
             if(!iterator.hasNext())
                 iterator = allStrings.iterator();
    
    
           }
       }
    
    }
    

    【讨论】:

    • 谢谢!这就是我想要的。我将如何将while(!scanner.nextLine().equals("exit")) 更改为我需要的?我目前拥有的是TextField 中的ActionListener,当用户按下 ENTER 时,它将进行搜索,然后继续搜索,如果他再次 ENTER,等等,
    • 迭代器效率较低。请改用流(JDK 8)或普通循环。您可以使用迭代器来更改循环中的集合。但由于没有这样做,迭代器是不必要的。
    • @FabioAbreu 在您的文本字段中,您可以添加ActionListner 并将while 循环作为lambda expression 传递。
    • @NemindaPrabhashwara 谢谢。我想我只是有一个空白,我可以像你说的那样在ActionListener 上简单地做while (!textField.getText().equals("exit"))
    【解决方案2】:

    您可以遍历您的列表,每次找到该字符串时,您都可以调用您的方法来突出显示它。或者,如果您希望每次调用您的方法时能够逐个获取字符串,那么您可以使用迭代器。在第一次调用您的方法时,您会获得列表的迭代器并将其存储在类的私有字段中。在后续调用中,您使用此迭代器从列表中获取下一个匹配字符串。

    【讨论】:

      【解决方案3】:
      public static void main(String[] args) {
      
          ArrayList<String> exampleList = new ArrayList<String>();
      
          exampleList.add("string1");
          exampleList.add("john");
          exampleList.add("string2");
          exampleList.add("arnold");
          exampleList.add("string3");
      
          String inputExample = "str";
          int count = 0;
          while(count >= 0){
              System.out.println(customSearch(exampleList, inputExample, count);
              count ++;
              } // write break statement when count reaches to a particular value based on your use case
      
      String customSearch(List<String> words, String searchKey, int count){
          List<String> searchedWords = words.stream().filter(word -> if word.toLowerCase().contains(searchKey.toLowerCase)).collect(Collectors.toList());
          return searchWords.get(count%searchedWords.size());
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-06-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-12
        • 1970-01-01
        相关资源
        最近更新 更多