Well, like you said, if you must have an index just use a standard for loop, or update a counter manually But rethink your question. Why should you need an index in a for (Object o : list) loop? The whole point of using an enhanced loop is to abstract away the nasty parts of using an index or an iterator Most languages today do not give this kind of functionality in loops.
Just as an example, the Pythonic way to do this is: for index, val in enumerate(someList).
Well, like you said, if you must have an index just use a standard for loop, or update a counter manually. But rethink your question. Why should you need an index in a for (Object o : list) loop?
The whole point of using an enhanced loop is to abstract away the nasty parts of using an index or an iterator. Most languages today do not give this kind of functionality in loops. Just as an example, the Pythonic way to do this is: for index, val in enumerate(someList): # ....
The enhanced for loop was created as a shortcut for when you don't need the index of the element. It streamlined things. If you still need the index, fall back to a regular for loop.
That's one of the reasons they're there.
Well consider iterating over a collection with generics. Its ridiculously long and hard to read when using an Iterator directly. Seems to me like it would be nice to retain the abstraction of the enhanced for-loop while simultaneously being able to access the index (if desired).
– oym Nov 29 '10 at 21:56.
1 +1 for being simple and straight forward. Also, for various reasons regarding implementation specific time complexities for Collection. Get(i), just adding the counter to the enhanced for loop body is probably the best alternative.As opposed to reverting back to a traditional for(statement;condition;statement) loop structure as previously suggested.
– Tim Bender Nov 29 '10 at 22:15.
It's better to keep an outside counter to get the index,but you could do something like this ( it's bit of an overkill for a simple task): class Enum implements Iterable { private int indx; private Iterator itr; private Indexer indxr = new Indexer(); private Enum(Iterable iter) { this. Itr = iter.iterator(); } public static Enum iterate(Iterable iter) { return new Enum(iter); } @Override public Iterator iterator() { return new AbstractIterator() { @Override protected Indexer computeNext() { if (itr.hasNext()) { indxr. Index = indx++; indxr.
Val = itr.next(); return indxr; } return endOfData(); } }; } } class Indexer { public int index; public T val; @Override public String toString() { return String. Format(" %d : %s ", index, val); } For getting index while while iterating: List list = new ArrayList() ; list. AddAll(Arrays.
AsList(1, 2, 3, 4, 5,6,55,23,12,24,16)); for (Indexer en : Enum. Iterate(list)) System.out. Println(en.
Index+" "+en. Val); Note:The above code has dependency to Guava's AbstractIterator.
There isn't way right now unless you keep a counter! I know this is ugly, but java 7 will be better. I don't know why other people are saying we don't need it.
Sometimes we need to know what is last member, or odd member, etc.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.