I have a program that has a list of items.
I then search for a string and try to find it in said list.
What I would want to happen is: It finds the first occurrence, then prints it out or saves it somewhere so I can use it later (by highlighting user choice for example). Then once I search for the same string again it should return me the second occurrence, then the third, etc. When it doesn't find any more occurrences it should go back to the start and repeat it.
This is a crude example of what I have:
public static void main(String[] args) {
ArrayList<String> exampleList = new ArrayList<String>();
exampleList.add("string1");
exampleList.add("john");
exampleList.add("string2");
exampleList.add("arnold");
exampleList.add("string3");
String inputExample = "str"
for (String s : exampleList) {
if (s.contains(inputExample)) {
System.out.println(s);
break;
}
}
}
I added the break inside the if so I can get only the first result. If I remove it I will get all results.
In the program I have similar code associated to the click of a button, highlighting the user choice if the list contains what the user searched for, this is just an example.
What happens in my program is that only the first result will be highlighted (because I broke out of the loop) or only the last result will be highlighted (because I didn't break out of the loop).
How can I make it so the search picks up where it left off, officially ignoring the results it already obtained?