Trouble with my enhanced for loop? [closed]

不问归期 提交于 2020-01-17 15:10:54

问题


so im having trouble understanding enhanced for loops..

Write a method called contains() that takes an array of integers and an integer value. The method should return true if the array contains the specified value, otherwise false.

boolean contains (int [] x, int y) {
    for (y : x) {

    }

}

i dont really get how they work i obviously need to return something?


回答1:


If you want to check whether the number y in contained within the array x, you should do the following:

boolean contains (int [] x, int y) {
    for (int i : x) {
       if (i == y) return true;
    }

    return false;
}

This type of for loop is often called for-each. The int i : x basically means Go through the loop 'x' and name the current variable 'i'. Then if the current variable i is equal to the one that you are looking for (in your case - y) then return true.




回答2:


enhanced for loop doesn't work without data type defined at left hand side of the colon(:) symbol.

for (int i : x)   // int(in this case) is necessary at left side and array type is necessary at right side
{

}

also to use user defined collection then you should have Iterable interface implemented to use foreach loop like you are using above. You will see these concept in Collection framework.




回答3:


First step: what is the algorithm?

You need to iterate over the items in the array, and check if one of them is y, the int you are looking for.

Second step: Use a normal for loop if it feels easier

I assume from your question that you are more comfortable with a normal for loop:

boolean contains (int [] x, int y) {
    for (int i = 0; i < x.length; i++) { //loop over each item in the array
        if (x[i] == y) return true; //if the i-th item in x is y,
                                    //you are done because y was in x
    }
    return false; //you have not found y, so you return false here
}

Third step: transform it into a for-each loop

boolean contains (int [] x, int y) {
    for (int item : x) { //in this loop, item is x[i] of the previous code
        if (item == y) return true;
    }
    return false;
}

Notes:

  • In your code, you are reusing y inside the loop, which is not what you want: y is what you are looking for and you need to compare it to the content of x.
  • the syntax of the for-each loop on an array is: for (Type item : array). You can't omit Type by declaring the variable before the loop for example.



回答4:


Using a normal for loop you would write something like this:

boolean contains (int [] x, int y) {
    for (int i=0; i<x.length; i++) {
         if(y == x[i] return true;
    }    
    return false;
}

With the enhanced loop, however, it can be simplified as follows.

boolean contains (int [] x, int y) {
    for (int k: x) {
       if(y == k) return true;
    }
    return false;
}

It is just a minor convenience.

You might find the following useful: https://blogs.oracle.com/CoreJavaTechTips/entry/using_enhanced_for_loops_with



来源:https://stackoverflow.com/questions/16646649/trouble-with-my-enhanced-for-loop

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!