This is a discussion on Keeping List member private, while using new 'for' syntax. - Java ; I have a class: class MyClass { private List<int> myList; public Iterator getIterator() { return myList.iterator(); } } When using an instance of MyClass, and I want to iterate over the contents of myList, I call getIterator(), and use that ...
I have a class:
class MyClass
{
private List<int> myList;
public Iterator getIterator() { return myList.iterator(); }
}
When using an instance of MyClass, and I want to iterate over the
contents of myList, I call getIterator(), and use that iterator.
I would like to be able to use the new 1.5 syntax:
List<int> ls;
for( int val : ls )
{
System.out.println("value is "+ val);
}
I dont' see anyway of doing this, without having myList public or a
public acessor method.
Any ideas?
I haven't used Java a programming language in 3 years, so I could be
missing the obvious here.
Thanks,
~S