import java.util.Iterator;
import java.util.Random;
public class SeqShuffle implements Iterable{
private int[] cards;
SeqShuffle(int cnt){
cards = new int[cnt];
for(int i=0; i<cnt; i++){
cards[i] = i;
System.out.println(cards[i]);
}
Random rd = new Random(System.currentTimeMillis());
for(int i=cnt-1; i>0; --i){
int j = rd.nextInt(i);
int temp =cards[i];
cards[i] = cards[j];
cards[j] = temp;
System.out.print("第"+(cnt-i)+"次变换:");
for(int k=0; k<cnt; k++){
System.out.print(cards[k]);
}
System.out.println();
}
}
public Iterator iterator(){
return new ItrSeqShuffle(this.cards);
}
}
_________________________________________________________________________________-
//ItrSeqShuffle.java
import java.util.Iterator;
public class ItrSeqShuffle implements Iterator {
private int curpos;
private int[] cards;
public ItrSeqShuffle(int[] cards){
curpos = -1;
this.cards = cards;
}
@Override
public boolean hasNext() {
// TODO Auto-generated method stub
return ++curpos < cards.length;
}
@Override
public Object next() {
// TODO Auto-generated method stub
return cards[curpos];
}
}
_______________________________________________________________________________
//TestShuffle.java
import java.util.Iterator;
public class TestShuffle {
public static void main(String[] args){
SeqShuffle seqShuffle = new SeqShuffle(7);
for(Object q:seqShuffle){
System.out.print(q);
}
System.out.println();
}
}