public class kmp { static boolean flag = true; public static void main(String[] args) { String haystack = "loloqwlololhlklllellllo"; String needle = "ol"; char arr1[] = haystack.toCharArray(); char arr2[] = needle.toCharArray(); int j = 0; for (int i = 0; i < arr1.length; i++) { if (arr1[i] == arr2[j]) { if (j == arr2.length -1) { System.out.println(arr2.length); System.out.println(i); flag = false; break; } if (j < arr2.length - 1) j++; } else if (arr1[i] != arr2[j]) { j = 0; continue; } } if (flag) { System.out.println(-1); } } }标签:...,String,System,length,arr2,arr1,必定,8.13,out From: https://blog.csdn.net/weixin_51721783/article/details/141161478