Skip to content

Instantly share code, notes, and snippets.

@johobemax
Created November 19, 2010 09:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johobemax/a3ab734ac681a7657ec2 to your computer and use it in GitHub Desktop.
Save johobemax/a3ab734ac681a7657ec2 to your computer and use it in GitHub Desktop.
import java.util.ArrayList;
import java.util.ListIterator;
class RandomList{
public static void main(String[] args){
ArrayList<Integer> list = new ArrayList<Integer>();
ListIterator<Integer> it;
for(int i=0; i<5; i++){ //=> 5回ループ
int rand = (int)(Math.random() * 10) + 1; //=> ランダム値
it = list.listIterator(); // リストイテレータ初期化
while(it.hasNext()){
if(rand < it.next()){
it.previous(); // イテレータの位置を一つ前に戻す
break; // ループを抜ける
}
}
it.add(rand); // itの場所の後ろに追加する
}
// listの内容を確認
System.out.println(list.toString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment