简体   繁体   中英

traversing arrays using for loops in java

I want to compare numbers from two arrays where array1[start] compares with array2 and makes comparisons with every array2 element and then increment the array1 index once its done so. Heres what ive got so far

for(int i=0; i<array1.length; i++){ // move along array 1 

    if(array1[i]>array2[j]){
        //do something
        }

  for(int j =0; j<array2.length; j++){ // moves along array 2
        }
}

This is probably what you wanted.

for(int i=0; i<array1.length; i++){ // move along array 1
    for(int j=0; j<array2.length; j++){ // moves along array 2
        if(array1[i]>array2[j]){
            //do something
        }
    }
}

Your placement of core logic block is in the wrong scope.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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