0

Is there a way to use a return method to return a specific element from an array using a loop?

For example if I want to return the element "these" during a loop. I know the loop will break but that is not an issue I just want a specific element from a specific index ("these"). String[ ] my_array = {"test" , "these", "arrays", "at sololearn"}; for (int newma: my_array) {    System.out.println(newma);

20th Sep 2017, 9:58 PM
Sibusiso Mbambo
1 Réponse
+ 4
If you already know the index of the element to get the item that you want, then it seems as though you don't really need the loop at all. I.E. return my_array[1] If you're wanting to loop through the array and check if the element is a certain string and return it if it is then you can do so like: for(String s: my_array) { if(s.equals("these")) return s; }
20th Sep 2017, 10:48 PM
ChaoticDawg
ChaoticDawg - avatar