如何在另一个数组中找到数组的出现?并返回第一个数组的索引

时间:2017-11-20 12:00:24

标签: java arrays

这是我想要完成的算法但不知道如何前进,

public int findArray(int[] array, int[] subArray) {
    for (int i = 0; i < array.length; i++) {
        for (int j = 0; i < subArray.length; j++) {
            if (array[i] == subarray[j]) {//not sure if this is how to start 

            }
        }//will want to compare all element in subarray to Array
    }
    return 0;
}

我想要的结果如下

//[4,9,3,7,8] and [3,7] should return 2.
//[7,8,9] and [8,9,10] should return -1

//[4,9,3,7,8,3,7,1] and [3,7]should return 5

我的理解是我整体采用子阵列并试图找到它在阵列中的匹配位置但不知道如何去做它

6 个答案:

答案 0 :(得分:2)

一种简单的方法:

// 1) Convert array to String, for eg. [1,2,3,4] => "1234". 
// 2) Use Strings substring/lastIndexOf to find the correct index. 

private static int findSubarrayPosition(int[] array, int[] subarray) {
      StringBuilder sb = new StringBuilder();
      for (int i = 0; i < array.length; i++) {
          sb.append(array[i]);
      }
      String string = sb.toString();

      sb = new StringBuilder();
      for (int i = 0; i < subarray.length; i++) {
          sb.append(subarray[i]);
      }
      String subString = sb.toString();

      return string.lastIndexOf(subString);
}

答案 1 :(得分:1)

尝试以下代码。您必须检查子数组中的第一个元素,然后检查其他元素。

public static int findArray(int[] array, int[] subArray) {
    int index = -1;
    arrayLoop : for (int i= 0; i<array.length; i++){
        if(array[i] == subArray[0]){
            for (int j=1; j<subArray.length; j++){
                if (i+j>array.length-1 || array[i+j]!=subArray[j]) continue arrayLoop;
            }
            index = i;
        }
    }
    return index;
}

使用以下代码检查您的测试用例。

public static void main(String[] args) {
    int[] array1 = {4,9,3,7,8};  int[] subArray1 = {3,7};
    System.out.println(findArray(array1, subArray1));

    int[] array2 = {7,8,9};  int[] subArray2 = {8,9,10};
    System.out.println(findArray(array2, subArray2));

    int[] array3 = {4,9,3,7,8,3,7,1};  int[] subArray3 = {3,7};
    System.out.println(findArray(array3, subArray3));

}

答案 2 :(得分:1)

public static int findArray(int[] array, int[] subArray) {
    int count = 0;
    for (int i= 0; i < array.length; i++){
        for(int j=0; j < subArray.length; j++) {
          if(array[i] == subArray[j]){
              if(array.length > (i + 1) && subArray.length > (j + 1)) {
                    if(array[i + 1] == subArray[j + 1]) {
                        count += 1;
                        continue;
                    }
                }else if(subArray.length == j + 1) {
                        count += 1;
                        continue;
                    }
            }
        }
    }
    if(count < subArray.length) {
        return -1;
    }
    return count;
}

你也可以这样做

答案 3 :(得分:1)

这是问题的答案:

public class FindInArray {

public static void main(String[] args) {

    int array[] = { 4, 5, 6, 7, 5, 8, 2, 3 };
    int subArray[] = { 2, 3 };

    System.out.println("Array position is: " + matcher(array, subArray));

}

public static int matcher(int[] array, int[] subArray) {

    int pos = 0;
    for (int i = 0; i < array.length; i++) {
        for (int j = 0; j < subArray.length; j++) {
            if (array[i] != subArray[j]) {
                break;
            } else if (array[i] == subArray[j]) {
                boolean checkAllElements = compareRestElement(array, subArray, i, j);
                if (checkAllElements) {
                    pos = i;
                    return pos;
                } else {
                    pos = -1;
                }
            }
        }
    }

    return pos;
}

public static boolean compareRestElement(int[] arr, int[] subA, int index, int matchedPos) {
    boolean allElmentsOk = false;
    for (int k = matchedPos; k < subA.length; k++, index++) {
        if (arr[index] == subA[k]) {
            allElmentsOk = true;
            continue;
        } else {
            allElmentsOk = false;
        }
    }

    return allElmentsOk;
}

}

答案 4 :(得分:0)

您可以逐步移动比较位置,找到匹配的序列:

private static int findSubarrayPosition(int[] array, int[] subarray) {

    int start = 0;
    while (start + subarray.length < array.length) {
        if (Arrays.equals(Arrays.copyOfRange(array, start, start + subarray.length), subarray)) {
            return start;
        }

        start++;
    }

    return -1;
}

可以使用以下呼叫测试:

public static void main(String[] args) {

    int[] array = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    int[] subarray = new int[] { 5, 6, 3 }; //returns -1
    int[] subarray = new int[] { 5, 6, 7 }; //returns 4

    System.out.println(findSubarrayPosition(array, subarray));
}

然后你知道它是否返回-1,你没有匹配;否则,subarray位于返回的索引处。

答案 5 :(得分:0)

MonadReader