Next element using for loops java -



Next element using for loops java -

very rudimentary question have loop e.g.

list<objectlist> = //set of values inside. for(object info : objectlist){ // how access next element? // current element accesed 'data'. index position , increment there easier way? }

how next element/previous? know there iterators can utilize , on want know neat way in loop.

make 2 methods next , pervious , pass list , element.

public static <t> t nextelement(list<t> list,t element){ int nextindex=list.indexof(element)+1; homecoming list.size()<nextindexlist?null:list.get(nextindex); } public static <t> t previouselement(list<t> list,t element){ int previousindex=list.indexof(element)-1; homecoming list.size()>previousindexlist?null:list.get(previousindex); }

1)first way

for(objectlist info : objectlist){ objectlist previouselement=previouselement(objectlist,data); objectlist nextelement=nextelement(objectlist,data); }

2) sec way

for(int i=0;i<=objectlist.size();i++){ objectlist previouselement=objectlist.size>i-1?null:objectlist.get(i-1); objectlist nextelement=objectlist.size<i+1?null:objectlist.get(i+1); }

3) 3rd way using iterator

java for-loop

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -