我大概知道你的意思,有点类似于福利彩票。不过你是遍历我就写个程序给你参考,如果有不谨慎的地方,请见谅- package test;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Random;
- public class Test12 {
- public static void main(String[] args) {
- List<String>list = new ArrayList<String>();
- list.add("1");
- list.add("2");
- list.add("3");
- list.add("4");
- int a = list.size();
- Random random = new Random();
- for (int i = a-1; i >=0; i--) {
- int randomNum;
- if (i>0) {
- randomNum = random.nextInt(i);
- }else {
- randomNum = 0;
- }
- String readyRemove = list.get(randomNum);
- if (i != 0) {
- System.out.print(readyRemove+",");
- }else {
- System.out.print(readyRemove);
- }
-
- list.remove(readyRemove);
-
- }
- }
- }
复制代码 |