开发者

How to detect the end of the loop (for each)?

Using for(int i = 0; i < str.length; i++) I can easily detect if the loop is at the end.

But I how can I know if I'm using for or for each.

for(String str : arrayString){

    if(End of for) //Do something if the end of the loop

}

The reason I use for(:) instead of for(;;) is that str is actually a large Object which I need to use that property for another loop inside that loop. 开发者_如何学JAVAAnd I don't like using much object.get(index).getProperty It just also for convenience to me in doing the coding. That's why I want to know if the for(:) loop is at the last index.

UPDATE

My interim solution is to assign a int variable that will hold the number of iteration. Then check if the variable is equal to the length of the str.length.


Two easy ways, which are somewhat amusing given your reasoning for using the foreach loop.

int i = 1;
for (String str : arrayString) {
    if (i++ == arrayString.length) {
        // end
    }
}

Or

for (int i = 0; i < arrayString.length; ++i) {
    String str = arrayString[i];
    if (i + 1 == arrayString.length) {
        // end
    }
}

Effectively the same thing. Just note the difference between the use of i within the if between the two loops and the starting value of i.


One option would be to write your own iterator implementation which exposed, at each point, properties of first, last, index and value.

I've done the same thing for .NET, and it was far from tricky. In Java it would be even easier, as you could use the hasNext() method of the original iterator to determine whether or not it's the last element. The code would look something like this (completely untested):

public SmartIterable<T> implements Iterable<SmartIterator<T>.Entry>
{
    private final Iterable<T> iterable;

    public SmartIterable(Iterable<T> iterable)
    {
        this.iterable = iterable;
    }

    public Iterator<T> iterator()
    {
        return new SmartIterator<T>(iterable.iterator());
    }
}

public SmartIterator<T> implements Iterator<SmartIterator<T>.Entry>
{
    private final Iterator<T> iterator;
    private int index = -1;

    SmartIterator(Iterator<T> iterator)
    {
        this.iterator = iterator;
    }

    public void remove()
    {
        // Could potentially just delegate
        throw new UnsupportedOperationException();
    }

    public boolean hasNext()
    {
        return iterator.hasNext();
    }

    public Entry next()
    {
        T nextValue = iterator.next();
        index++;
        return new Entry(nextValue, index);
    }

    public class Entry
    {
        private final int index;
        private final T value;

        private Entry(int index, T value)
        {
            this.index = index;
            this.value = value;
        }

        public T getValue()
        {
            return value;
        }

        public int getIndex()
        {
            return index;
        }

        public boolean isFirst()
        {
            return index == 0;
        }

        public boolean isLast()
        {
            // Call into containing instance
            return !hasNext();
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜