-
-
Save johobemax/a3ab734ac681a7657ec2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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