没有匹配时陷入循环 - Java

Stuck in a loop when there is no matching - Java

所以在这里我坚持我的程序,这是一件简单的事情。所以基本上在搜索方法中我有 3 个变量,如您所见,Char P char T 和 List pos。在方法下方,您可以看到有一个 Main,其中说明了 char P、char T 的值,我的想法是制作一个匹配程序,在其中输入值 P 并查看 char T 中是否有相同的值。它发现,它应该继续,直到价值完成。所以在找到结果后的主要方法中,它应该给你值设置到什么位置的索引。

现在的问题是,如果我给出一个正确的值示例 P =abc T = abcqweqweabc 它会给我一个正确的,但是如果我做 abcqweqweqwe 它会一直循环并且永远不会结束这会杀死我的处理器和ram(呵呵),我一直在想办法,但我找不到问题所在。看起来它应该可以工作,但我不明白它为什么喜欢它。

public class StringSearch {
    public static void search(char[] P, char[] T, List<Integer> pos) {
        int i = 0;
        int j;
        while(i!=P.length){ 
            for (j= 0; j<T.length; j++){ 
                if(P[i] == T[j]) { 
                    i++;
                    if(i == P.length) { 
                        pos.add((j-(P.length-1)));
                        i = 0; 
                        if(j==T.length-1) { 
                            i = P.length;
                        }
                    }
                }
                else {
                    i = 0;
            }
        }
    }

    public static void main(String[] args) {
        ArrayList<Integer> where = new ArrayList<Integer>();
        search("abcx".toCharArray(), "abcdabcxxabctx".toCharArray(), where);
        for (int i : where) {
            System.out.println(i); 
        }
    }
}

也请!如果你需要知道什么。只需评论,我可能会在一分钟内回复您!也请随时提问!

编辑:为了准确说明我正在尝试做的事情,我们的 Char T 是 "text" 所在的位置,char P 是我想要匹配 P 和 T 的字母所在的位置. 因此,每当我们找到 char P 和 T 之间的正确匹配时,就应该将其添加到我们称为 pos 的列表中。稍后会给我们一个结果,其中索引中的匹配是相同的。比如 T = qweabcqwe 和 P = abc。它应该给我们 4 的结果。所以我要做的是查看这两者之间是否存在匹配以及它们在哪个位置相同,如果存在,则放入列表然后循环在主要方法会告诉我们在哪里。

编辑第 2.3 部分:

public static void search(char[] P, char[] T, List<Integer> pos) {

            for (int i=0;i<=T.length-P.length;) { 
                if (T[i] == P[0]) {
                    boolean match=true;
                    for (int j=0;j<P.length;j++) {
                        if (T[i+j] != P[j]) {
                            match = false;
                            break;
                        }
                    }
                    if (match) {
                        pos.add(i);
                        i = P.length -1;

                    }
                }
            }
        }

        public static void main(String[] args) {
            ArrayList<Integer> where = new ArrayList<Integer>();
            search("abcx".toCharArray(), "abcdabcxxabctx".toCharArray(), where);
            for (int i : where) {
                System.out.println(i); 
            }
        }
    }

即使有一种情况

if(P[i] != T[j])

然后你将 i 设置为 0

else {
  i = 0;

你会陷入 while 循环

但基本上您尝试做的是给您与 indexOf()

相同的结果

试试这个:

String Str = new String("qweabcqwe");
String SubStr1 = new String("abc");

System.out.println( Str.indexOf( SubStr1 )); // will print 3

如果您想自己实现,您可能想查看使用 Java 详细解释的 KMP 算法,给您一些提示:http://tekmarathon.com/2013/05/14/algorithm-to-find-substring-in-a-string-kmp-algorithm/

要找到每个数组中相同位置的每个相同字符的索引,只需执行以下操作:

for (int i=0;i<P.length && i<T.length;i++) {
   if (P[i] == T[i]) {
       pos.add(i);
   }
}

你只需要一个循环,因为你只比较一个值。

如果你想找到子字符串的位置,那么你确实需要一个嵌套循环,但它仍然可以是一个简单的 for 循环。

for (int i=0;i<=P.length-T.length;i++) { // Can stop when we get to within T length of the end of P
   if (P[i] == T[0]) {
       boolean match=true;
       for (j=0;j<T.length;j++) {
          if (P[i+j] != T[j]) {
             match = false;
             break;
          }
       }
       if (match) {
           pos.add(i);
           // You might want to add T.length-1 to i here to cause it to skip, it depends if you want to find overlapping results or not. (i.e. searching for bobob in bobobob.
       }
   }
}