Given a generic class:
class MyClass (
private List l = new LinkedList <String>();
public void addElement (String s) (l.add (s);)
.............
)
an accessor method that allows me to iterate on the list as it should be?
I had decided to implement a method that returns an iterator directly, but does not seem correct because it could change the list from the outside with remove ().
What do you think?
If you don’t mind exposing the fact that the elements are stored as a List, you could also use do:
If you want to allow callers to use the “foreach” syntax, you might want to return an Iterable:
And, again, if you don’t mind exposing that the elements are returned as a List, this last solution could return
List<String>