Hi, I'm a C++ guy, and using iterators in C++ is simple.
Unfortunately, they work a lot differently in Java. :-(
In C++, I'd do something like this:
I tried doing this in Java:
But, it never prints the last index of the vector since the hasNext() returns false before the last string is pulled out of the iterator.
What's the Java equivalent of what I'd normally do in C++?
Unfortunately, they work a lot differently in Java. :-(
In C++, I'd do something like this:
Code:
vector<string> strings;
...
for ( iterator<string> it = strings.begin(); it != strings.end(); ++it )
{
cout << *it << endl; // Print each string on a new line.
}
Code:
Vector<String> strings;
...
iterator<String> it = Vector.iterator();
for ( String str = it.next(); it.hasNext(); str = it.next() )
{
System.out.println( str );
}
What's the Java equivalent of what I'd normally do in C++?