Skip to content
Advertisement

Tag: foreach

Is there a way to go back by one iteration in a for-each loop?

For example, if I were to do… If it isn’t possible, is there another method of traversing through some sort of collection class while controlling the iteration counter? I tried looking through the answers to this question but couldn’t think of a way that was clear to me. Any suggestions are appreciated! Answer It depends on what you are trying

Difference between Arraylist.forEach() and Arraylist.replaceAll()

I have a question about the difference between Arraylist.forEach() and Arraylist.replaceAll(). I know that Arraylist.forEach() is used to perform an action for each element in the list, but it seems that Arraylist.replaceAll() does the exact same thing. But for some reason, when you decide to do a.forEach(e -> e.concat(“test”); assuming a is an Arraylist of Strings, the Arraylist does not

How can I make my class iterable so I can use foreach syntax?

I have Book and BookList classes. BookList is something like this: In my main class I want to print titles of books with in a for each loop: Eclipse says: Can only iterate over an array or an instance of java.lang.Iterable How can I get this working? Answer You need to implement the Iterable interface, which means you need to

Advertisement