jeudi 15 septembre 2016

Return an element of a for-loop after verifying a condition

How can I do to stop a for loop and return an element satisfiying a if-condition ?

private List<String> list = new ArrayList<String>();
//we only neeed one good element of the list
    String find(){
        for (int i=0; i<list.size(); i++){
            if (list.get(i).isGood()) {
                return list.get(i);
            }
        }
}

It doesn't compile because there is no return statement. I would like to return list.get(i).

Aucun commentaire:

Enregistrer un commentaire