您应该遍历“目标”数组中的所有元素,并在“键”数组中的当前匹配模式之后保留一些索引,我们将此索引称为“keyIndex”。每次“目标”数组中的元素等于“键”数组中“keyIndex”位置的元素时,您增加 keyIndex(当前匹配模式更大)并添加到一些数据结构(我选择列表) 来自“目标”数组的索引,其中元素相等。如果元素不相等,您应该重置“keyIdnex”(当前匹配模式的长度为零)并清除列表。我相信这应该对你有好处: public static List<Integer> findPattern(int[] key , int[] target){ List<Integer> result = new ArrayList<Integer>(); //This list hold the indexes of the patter int keyIndex = 0; //The index to follow after the "key" array for(int i = 0 ; i < target.length; i++){ if(target[i] == key[keyIndex]){ //This "key" element is equal to the element from the "target" result.add(i); //Add the index in which the elements are equal. keyIndex++; //The currently match pattern is larger, increment "keyIndex" if(result.size() == key.length) //The all pattern is checked and match, return the list which store the indexes return result; }else{ //The pattern is not match anymore, reset all the data keyIndex = 0; i--; result.clear(); } } return null; //The pattern from "key" not found in "target" ,return null }