来自arraylist的java匹配字符串,其中该字符串是来自arraylist的串联结果

hk8txs48  于 2021-07-09  发布在  Java
关注(0)|答案(3)|浏览(342)

我试图找出匹配字符串(称为userinput)的最佳方法,其中字符串是其他几个字符串(arraylist或array,在示例中我称之为approved)串联的结果

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

如果以下是用户输入,程序将返回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

但是,如果以下是用户输入,程序将返回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

我正在考虑拆分用户输入以获得firstword和lastword,因为顺序很重要。然后,使用contains在arraylist中找到它们的索引。
比如说

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

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

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

由于顺序很重要,这里的逻辑是firstwordpossibleindex包含的值应该低于lastwordpossibleindex,因此,如果有任何更大的值,可以将其删除,因为提供的字符串将无效。
在实现该逻辑之后,它应该开始匹配firstwordpossibleindex到lastwordpossibleindex的下一个索引
在本例中,它将检查userinput中的第二个单词,并尝试与索引为2和5的元素匹配(因为firstwordpossibleindex是1和4)
它将一直检查到lastwordpossibleindex,如果所有单词都按照arraylist排序,它将返回true。
在这种情况下,我仍然无法匹配与另一个字符串的一部分连接的字符串。你有办法解决这个问题吗?
有图书馆可以解决这个问题吗?

1rhkuytd

1rhkuytd1#

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+" ");
}
q5iwbnjs

q5iwbnjs2#

首先,您应该将“def jlk ggwp my life”这样的条目拆分为每个部分(本例中为5个),并将它们分别添加到列表中。然后,将用户的输入按空格分割,并将其保存到一个数组中
approved.contains(输入数组的元素)
如果数组中的所有元素都存在于approved arraylist中,则返回true。如果其中任何一个不在已批准的列表中,则返回false。

v2g6jxz6

v2g6jxz63#

您可以尝试以下方法:

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

相关问题