开发者

Java going through a list?

How would I make java go through a list in order?

Example: Im trying to get 2 diff coords but If I make it load 1 then it still needs to load 2 ints, so I'm making it load 2 but if its random it could choose random coords and开发者_JAVA技巧 screw up, BUT if I make it go in order, both coord lists will remain in order and it will work, how would I do this?


If you have a list you can iterate its elements in order using its Iterator...

List<Integer> myCoords = new ArrayList<Integer>();
myCoords.add(10);
myCoords.add(20);
Iterator<Integer> myListIterator = someList.iterator(); 
while (myListIterator.hasNext()) {
    Integer coord = myListIterator.next();     
}

Java List Documentation

Java Iterator Documentation


It seems as if: you have n element collection of type Integer instances, and you want a random size m element permutation; where m <= n.

Basically you are looking for java.util.Collections.shuffle() method.

    List<Integer> nrs = Arrays.asList(new Integer[] { 3, 5, 6, 9, 12 });
    Collections.shuffle(nrs);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜