【问题标题】:Matching string from arraylist where the string is the result of concatenation from the arraylist匹配来自 arraylist 的字符串,其中字符串是来自 arraylist 的连接结果
【发布时间】:2017-12-19 17:20:54
【问题描述】:

我试图找出匹配字符串的最佳方法(称为 userInput),其中字符串是来自其他几个字符串(数组列表或数组,在示例中我称为批准

ArrayList<String> approved = new ArrayList<>();
approved.add("abc");
approved.add("def");
approved.add("ghi");
approved.add("def jkl ggwp my life"); //repeated elements (abc,jkl)
approved.add("jkl");
approved.add("mno");
approved.add("pqr");
approved.add("stu vwx");
approved.add("yz");

为了解释我的困难,我将使用这个数组列表(上图)。但是,在现实世界中,我有一个

    -fixed arraylist which wont have dynamic elements (the elements in the arraylist wont change)
    -arraylist with more than 6000 elements
    -elements in the arraylist contains multiple word e.g ("stu vwx")
    -repeated elements but concatenated with another string in the arraylist

如果以下是 userInput

,则程序应返回 true
userInput = "abc def";
userInput = "stu vwx yz"; //they can combine with another element as a whole
userInput = "ghi"; //it doesnt have to combine with another element
userInput = "vwx yz"; //they can split the arraylist elements with whitespace only and concatenate it with another element

但是,如果以下是 userInput

,程序将 返回 false
userInput = "yza"; //obviously it doesnt match anything
userInput = "ghi jk"; //doesnt match the concatenated string (ghi and jkl)
userInput = "pqr stu v"; //it can split the element with whitespace,but it has to take the whole word
userInput = "def abc"; //the order are important

我正在考虑拆分 userInput 以获得 firstWordlastWord 因为顺序很重要。然后,使用 contains 在数组列表中查找它们的索引。

我们说

String userInput = "def ghi jkl mno";
//so,the firstWord and lastWord will be
firstWord = "def";
lastWord = "mno";

从这里开始,.contains() 将完成其工作并返回具有字符串 firstWordlastWord 的元素的多个索引(由于firstWord在arraylist中出现了多次),它将被配置为返回另一个数组中可能的匹配项。

firstWordPossibleIndex[] = {1,4};
lastWordPossibleIndex[] = {6};

由于顺序很重要,这里的逻辑是 firstWordPossibleIndex 应该包含比 lastWordPossibleIndex 小的值,所以如果有更大的值,可以将其删除因为提供的字符串是无效的。

在实现该逻辑之后,它应该开始匹配从 firstWordPossibleIndexlastWordPossibleIndex

的下一个索引

在这种情况下,它会检查 userInput 中的第二个单词并尝试匹配索引为 2 和 5 的元素(因为 firstWordPossibleIndex 是 1 和4)

它会检查直到lastwordPossibleIndex,如果所有的单词都按照arraylist的顺序,它会返回true。

有了这个,我仍然无法将它连接的字符串与另一个字符串的一部分进行匹配。你有什么办法解决这个问题吗?

有没有图书馆可以解决这个问题?

【问题讨论】:

    标签: java android json string-matching


    【解决方案1】:

    首先,您应该将“def jlk ggwp my life”之类的条目拆分为每个部分(在本例中为 5 个),并将它们分别添加到列表中。然后,用空格分割用户的输入并将其保存到一个数组中,然后就可以了

    approved.contains(输入数组的元素)

    如果数组中的所有元素都存在于已批准的arraylist 中,则返回true。如果其中任何一个不在批准列表中,则返回 false。

    【讨论】:

      【解决方案2】:

      你可以试试这样的:

      import java.util.ArrayList;
      import java.util.List;
      
      public class WhatSoEver{
      
          static int order;
      
          public static void main(String[] args) {
      
              ArrayList<String> approved = new ArrayList<>();
              approved.add("abc");
              approved.add("def");
              approved.add("ghi");
              approved.add("def jkl ggwp my life");
              approved.add("jkl");
              approved.add("mno");
              approved.add("pqr");
              approved.add("stu vwx");
              approved.add("yz");
      
              System.out.println(isValid(approved, "abc def")); // true
              System.out.println(isValid(approved, "stu vwx yz")); // true
              System.out.println(isValid(approved, "ghi")); // true
              System.out.println(isValid(approved, "vwx yz")); // true        
      
              System.out.println(isValid(approved, "yza")); // false
              System.out.println(isValid(approved, "ghi jk")); //false
              System.out.println(isValid(approved, "pqr stu v")); //false
              System.out.println(isValid(approved, "def abc")); //false
      
          }
      
          public static boolean isValid(List<String> approved, String userInput){
              order=0;
              for(String word : userInput.split(" ")){
                  if(!containsWord(approved, word)){
                      return false;
                  }
              }
              return true;
          }
      
          private static boolean containsWord(List<String> approved, String word){
               for(int i=0; i<approved.size(); i++){
                  for(String subS : approved.get(i).split(" ")){
                     if(word.equals(subS) && (i+1)>order){
                            order=i;
                            return true;
                     }
                  }
               }
               return false;
           }
      }
      

      输出

      true
      true
      true
      true
      false
      false
      false
      false
      

      【讨论】:

        【解决方案3】:
        public static void main(String[] args) {
            ArrayList<String> approved = new ArrayList<>();
            approved.add("abc");
            approved.add("def");
            approved.add("ghi");
            approved.add("def jkl ggwp my life"); //repeated elements (abc,jkl)
            approved.add("jkl");
            approved.add("mno");
            approved.add("pqr");
            approved.add("stu vwx");
            approved.add("yz");
        
            String[] userInput ={"abc def","stu vwx yz","ghi","vwx yz","yza","ghi jk","pqr stu v","def abc"};
            for(String str: userInput){
                System.out.println(str+"\t"+check(str,approved));
            }
        }
        
        public static boolean check(String userInput, ArrayList<String> list){
            String joined = String.join(" ", list)+" ";        
            return joined.contains(userInput+" ");
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-09-03
          • 2020-02-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-02-18
          相关资源
          最近更新 更多