D
D
DR_Demons2014-11-21 23:10:26
Java
DR_Demons, 2014-11-21 23:10:26

How to select the next element of a Java Collection object?

Good day! How to get the next element of a collection? There is a collection of objects, the object has two fields name and code (respectively, the get and set methods for them), the names need to be compared with each other, this is the responsibility of the method that takes two lines (the line and the line following it)

List<SomeClass> list = new ArrayList<SomeClass>();
        for (SomeClass str : list) {
         \\Строку я могу получить, но как получить следующую за ней строку?
         someMethod(str.getName(), /*?????*/);
    }

Thanks in advance!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Y
Yaroslav Astafiev, 2014-11-21
@DR_Demons

In this case, you need to use a regular loop, not a for-each loop

for(int i=0; i < list.size()-1; i++){
     someMethod(list.get(i), list.get(i+1));
}

B
bromzh, 2014-11-21
@bromzh

Learn iterators. In your case, you need a while and next loop to get the next element.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question