简体   繁体   中英

Java redundant second if statement in for loop

I'm new to java and inspired by this discussion I wrote a while loop version here and a for loop version (this question) of a program that searches an array for an "item" (int 20, as called in the main method) and returns its value, and they both work.

However, in this for loop version the second if statement, especially "database[i] == database[database.length-1]", looks redundant because the termination condition in the for loop already specifies that i < database.length.

class For {

static int [] database = {17,18,19,20,21};

public static int findItem(int item) {

    for ( int i=0; i<database.length; ++i ) {
        if ( database[i] == item ) {
            System.out.println("Item found at position: " + i);
            return i;
        }
        if ( database[i] == database[database.length-1] && database[i] != item )
            System.out.println("Item not found.");
    }
    return item;
}

public static void main(String [] args) {
    findItem(20);
}}

I tried swapping the second if with an else statement but then the for loop evaluated every value in the array before 20 and printed "Item not found" three times.

Is this an acceptable use of a for loop and if statements or is it completely redundant?

If I understand correctly, you want to print a message if the item is not in the array. This should not be done inside the for loop, since, at this point, you haven't processed the whole array yet. It should be done after the for loop:

for (int i = 0; i < database.length; i++) {
    if (database[i] == item) {
        System.out.println("Item found at position: " + i);
        return i;
    }
}
System.out.println("Item not found.");
return item;

Now, returning the item if it was not found is a terrible idea. The caller will assume that the item has been found at the index item , which is incorrect. You should return a special value (or throw an exception) if the item wasn't found, that can't be confused with a valid index. For example: -1 (which is the traditional value returned in such cases):

for (int i = 0; i < database.length; i++) {
    if (database[i] == item) {
        System.out.println("Item found at position: " + i);
        return i;
    }
}
System.out.println("Item not found.");
return -1;

Now, it should not be the responsibility of this method to print the result it returns. The caller should do that:

public static int findItem(int item) {
    for (int i = 0; i < database.length; i++) {
        if (database[i] == item) {
            return i;
        }
    }
    return -1;
}

public static void main(String[] args) {
    int index = findItem(20);
    if (index >= 0) {
        System.out.println("Item found at position: " + index);
    }
    else {
        System.out.println("Item not found");
    }
}

You could simplify the method by moving the "not found" message after the loop, as follows:

public static int findItem(int item) {

    for (int i = 0; i < database.length; i++) {
        if ( database[i] == item ) {
            System.out.println("Item found at position: " + i);
            return i;
        }   
    }
    System.out.println("Item not found.");
    return -1; // not found
}

Because you've already exited the method with return inside the loop, if the number was found.

Also note that I've changed the method to return -1 in case the number was not found. This is a common practice to indicate that a search found no results.

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