猿问

Java匹配两个字符串之间的每两个序列字符

我的列表中的数值很少,所以我需要从左到右匹配每两个序列号/字符。


在下面,stringList我的记录很少,我必须将这些记录与另一个记录进行比较,即wordA.


我想匹配每两个序列号/字符,所以如果我输入630102它必须返回第三个。如果我输入630677它必须返回第二个,因为630677它不存在于我的stringList但这里第一个4数字6306与我的列表值匹配。


以下是我尝试过的示例代码。如果您有任何建议,请提供。


String wordA = "630102"; 

List<String> stringList = new ArrayList<>();

stringList.add("630507");  //1st

stringList.add("630622");  //2nd

stringList.add("6301");    //3rd

stringList.add("63");      //4th

String common = "";


int count1 = 0;

int count2 = 0;

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

    for (int j = 0; j < wordA.length(); j++) {

        if (stringList.get(i).length() > j) {

            if (wordA.charAt(j) != stringList.get(i).charAt(j)) {

                break;

            } else if (wordA.charAt(j) == stringList.get(i).charAt(j)) {

                count1++;


                if (count1 == 2) {

                    count2++;

                    count1 = 0;

                }

            }

        }

    }

    if (wordA.length() > stringList.get(i).length()) {

        common = stringList.get(i);

        break;

    }

}

System.out.println("common is: " + common);


拉丁的传说
浏览 206回答 2
2回答

眼眸繁星

关于如何解决这个问题,基本上有两种基本方法:For-对列表和每个字符的每次迭代你离这个人很近。干得好:int maxLength = 0;&nbsp;int indexOfLongestMatch = -1;for (int i = 0; i < stringList.size(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Iterate the List&nbsp; &nbsp; String string =&nbsp; stringList.get(i);&nbsp; &nbsp; int maxIndex = Math.min(wordA.length(), string.length());&nbsp; // Compare lengths&nbsp; &nbsp; if (maxIndex >= maxLength) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Worth to continue?&nbsp; &nbsp; &nbsp; &nbsp; int commonLength = 0;&nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < maxIndex; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Iterate characters&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (string.charAt(j) == wordA.charAt(j)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; commonLength++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Any match counts&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (commonLength >= maxLength) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Check for the new max&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; maxLength = commonLength;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Register it&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; indexOfLongestMatch = i;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// New index of the max&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}中最长匹配词的索引stringList是indexOfLongestMatch(等于2根据您提供的相同输入)和词是stringList.get(indexOfLongestMatch)( 6301)。正则表达式。正如@YCF_L 在这里建议的那样,另一种可能的方法是 Regex,它可以帮助您仅匹配具有使用模式的公共初始字符的字符串:6?3?0?1?0?2?要从输入中实现此模式,您必须将每个字符替换.为$0?,其中$0任何与点匹配的匹配字符.。在Regex101 上演示Pattern pattern = Pattern.compile(wordA.replaceAll(".", "$0?"));然后您只需在列表上使用一次迭代找到最长匹配的字符串及其索引:Matcher matcher;int indexOfLongestMatch = -1;int maxLength = 0;for (int i = 0; i < stringList.size(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Iterate the List&nbsp; &nbsp; matcher = pattern.matcher(stringList.get(i));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Apply the pattern&nbsp; &nbsp; if (matcher.matches()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Has a match?&nbsp; &nbsp; &nbsp; &nbsp; int length = matcher.group(0).length();&nbsp; &nbsp; &nbsp; &nbsp; if (length >= maxLength) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Check for the new max&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; maxLength = length;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Register it&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; indexOfLongestMatch = i;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // New index of the max&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}结果与上面的示例相同。indexOfLongestMatch是找到的索引。请注意,默认情况下,结果索引设置为-1列表中不存在的索引 - 这意味着没有任何字符串以您想要的方式与输入匹配。

幕布斯6054654

据我了解,您想返回值,该值与您的单词最常见。这是我对您的问题的解决方案:&nbsp; &nbsp; String wordA = "630677";&nbsp;&nbsp; &nbsp; List<String> stringList = new ArrayList<>();&nbsp; &nbsp; stringList.add("630507");&nbsp; //1st&nbsp; &nbsp; stringList.add("630622");&nbsp; //2nd&nbsp; &nbsp; stringList.add("6301");&nbsp; &nbsp; //3rd&nbsp; &nbsp; stringList.add("63");&nbsp; &nbsp; &nbsp; //4th&nbsp; &nbsp; String common = "";&nbsp; &nbsp; int commonCount = 0;&nbsp; &nbsp; int highestCount = 0;&nbsp; &nbsp; for(String s : stringList)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; for(int i = 0; i < wordA.length(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(s.length() > i && s.charAt(i) == wordA.charAt(i)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; commonCount++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(s.length() <= i || s.charAt(i) != wordA.charAt(i) || wordA.length() <= i + 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(commonCount > highestCount) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; highestCount = commonCount;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; common = s;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; commonCount = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; System.out.println(common.isEmpty() ? "There is no common." : "common is: " + common);}编辑:请注意,这将始终返回列表中的第一个字符串,以防有更多字符串具有相同数量的正确字符。
随时随地看视频慕课网APP

相关分类

Java
我要回答