Randomly iterate over ArrayList<Integer> in Java -
seems basic question. i've arraylist<integer> al
, iterate on it. normally,
for(int : al) { // code }
does trick. requirement iterate not in sequence randomly.
you can use collections.shuffle()
on list.
note shuffle list itself, if order important should make copy of (and shuffle copy).
alternatively create random array has elements 0 - list.size()-1
, using indices access "random" elements in list
.
Comments
Post a Comment