简体   繁体   English

Java嵌套循环逻辑错误

[英]Java nested loop logic error

I want to know what is wrong with my logic when my output is supposed to be as follows: 我想知道当我的输出如下时我的逻辑出了什么问题:

There are two arrays of integers and that prints the index of the first occurrence of the first list in the second list.For example, suppose that you have these arrays: 有两个整数数组,它们打印第二个列表中第一个列表第一次出现的索引,例如,假设您具有以下数组:

int[] list1 = {1, 3, 6};
int[] list2 = {1, 3, 5, 8, 12, 1, 3, 17, 1, 3, 6, 9, 1, 3, 6}; 

Then the call indexOf(list1, list2) should return 8 because the sequence of values stored in list1 appears in list2 starting at index 8. The list1 appears twice in list2 , starting at position 8 and starting at position 12. The method should return the first such position. 然后,呼叫indexOf(list1, list2)应返回8因为存储在值的序列list1显示在list2开始于索引8. list1中出现两次list2 ,开始在8位和在位置12开始的方法应返回首先是这样的位置。

Currently, my code does not print anything... 目前,我的代码无法打印任何内容...

public static void indexOf(int[] arr1, int[] arr2){

    for(int i = 0; i < arr2.length; i++){
        for(int j = 0; j < arr1.length; j++){
            if(arr1[j] != arr2[i]){
                break; 
            }
            if(j == arr1.length -1){
                System.out.println(i);
                break;
            }
        }
    }
}

arr1[j] != arr2[i] should be arr1[j] != arr2[i + j] arr1[j] != arr2[i]应该是arr1[j] != arr2[i + j]

Why 为什么

In each iteration of the inner loop you should be comparing each element of the original subsequence ( arr1[j] ) with the corresponding element in the current subsequence you have sliced from arr2 ( arr2[i + j] ). 在内部循环的每次迭代中,您应该将原始子序列( arr1[j] )的每个元素与从arr2arr2[i + j] )切出的当前子序列中的对应元素进行比较。 You were comparing to just the first element in the current slice. 您只与当前切片中的第一个元素进行比较。

Moreover 此外

Your loop termination condition should be i + (arr1.length - 1) < arr2.length to avoid accessing out of bound index if the last element in arr2 was 1 (or in general equal to the first element in arr1 ). 您的循环终止条件应为i + (arr1.length - 1) < arr2.length以避免在arr2的最后一个元素为1(或通常等于arr1的第一个元素)时访问超出范围的索引。

...also ...也

The second break should be return to print the first occurrence as you stated. 如您所述,第二个break应该return以打印第一个break

Full Code 完整代码

public static void indexOf(int[] arr1, int[] arr2) {

    for(int i = 0; i + (arr1.length - 1) < arr2.length; i++) {
        for(int j = 0; j < arr1.length; j++) {
            if(arr1[j] != arr2[i + j]) {
                break; 
            }
            if(j == (arr1.length - 1)){
                System.out.println(i);
                break; // break to print all the occurrences. return to print only the first.
            }
        }
    }

This works (I added i+j instead of j): 这有效(我添加了i + j而不是j):

   public static void indexOf(int[] arr1, int[] arr2){

    for(int i = 0; i < arr2.length; i++){
        for(int j = 0; j < arr1.length; j++){
            if((i + j) < arr2.length && arr1[j] != arr2[i + j]){
                break; 
            }
            if(j == arr1.length -1){
                System.out.println(i);
                break;
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM