SOP
SOP

Reputation: 801

Iterate from specific index

This loop iterates from list's beginning to its last index but I want to iterate from any specific index.

 for (Object ob : objlist) 
    {
    //code
    }

I don't want to iterate using list.size(). I want to iterate from any specific list index and onwards...

Upvotes: 1

Views: 1325

Answers (3)

FredK
FredK

Reputation: 4084

For whatever type T is in your list,

ListIterator<T> iterator = objList.listIterator(index);
while( iterator.hasNext() ) {
  T item = iterator.next();
  // do stuff
}

Upvotes: 2

rgettman
rgettman

Reputation: 178263

You can have a standard for loop:

for (int i = start; i < objList.size(); i++)

or you can use an enhanced for loop on a sublist, using List's subList method:

for (Object ob : objList.subList(start, objList.size()))

Upvotes: 4

Jashaszun
Jashaszun

Reputation: 9270

They you probably want a vanilla for loop:

for (int i = startIndex; i < objList.size(); i++)
{
    Object ob = objList.get(i);
    // code
}

Upvotes: 3

Related Questions