2017-02-16 26 views
1

我有一个Java中的整数Arraylist调用列表,其中包含7个元素[12, 13, 17, 18, 19, 11, 20]。我想用随机顺序在列表的最后复制相同的元素两次,所以最后有三次相同的整数(总共21项),然而,每7项随机顺序。我如何在Java中这样做?复制和随机化列表中的项目的位置

+0

我不明白你的问题,你打算以21 int列表结束,还是只是在其他地方复制你随机选择的数字? – rakwaht

+0

是否重要,如果我们先复制它们然后随机化它们,或者你想要它,因为你复制它同时随机化。 –

+1

您可以将元素复制到另一个ArrayList,您将在其中执行'Collections.shuffle(copyList);'然后您将这些混合副本添加到原始数组列表中。 – matoni

回答

1

只需复制清单并随机播放每次迭代。

final int DUPLICATE_AMOUNT = 3; // if you want the created array 3 times as big as the original 

List<Integer> list = getMyList(); //original list 
List<Integer> fullRandom = new ArrayList<Integer>(); 
fullRandom.addAll(list); 
for (int i = 1; i < DUPLICATE_AMOUNT; i++) { 
    List<Integer> randomList = new ArrayList<Integer>(); 
    randomList.addAll(list); 
    Collections.shuffle(randomList); 
    fullRandom.addAll(randomList); 
} 
+0

我的初始列表是一个ArrayList。是否有可能首先将它转换为List以执行addAll? – konstantin

+0

虽然技术上是正确的,但我发现你使用'DUPLICATE_AMOUNT'和'int i = 1'具有误导性。 1)“重复”可以描述额外的副本或整体(如你所期望的)2)人们习惯于'int i = 0',以至于他们可能会错过你的'int i = 1'并且误解。你的答案没有错,但我认为'ADDITIONAL_COPIES = 2' +'int i = 0'会更清楚。 – Aaron

+1

@konstantin ArrayList是'List'的一个实现,不需要投射。 – Aaron

1

只要复制列表两次,将它洗:

List<Integer> tempList = new ArrayList<>(yourList); 

for(int i = 0; i < 2; i++){ 
    Collections.shuffle(tempList, new Random(System.nanoTime())); 
    yourList.addAll(tempList); 
} 
+1

@Aaron Thx供您参考。我编辑过它。 – Christian

1
// init your list 
List<Integer> initialList = new ArrayList<Integer>(); 
initialList.add(new Integer(12)); 
initialList.add(new Integer(13)); 
initialList.add(new Integer(14)); 
initialList.add(new Integer(15)); 
// create a new list that'll contain your random numbers 
List<Integer> tripleList = new ArrayList<Integer>(); 
// triple your values 
tripleList.addAll(initialList); 
tripleList.addAll(initialList); 
tripleList.addAll(initialList); 
// randomize their order 
Collections.shuffle(tripleList); 
// until is empty get the top of the list with this command. 
//A random number among your list 
tripleList.remove(0); 
1

尝试使用Collections.shuffle(),并调用它的两倍初始数据列表:

private List<Integer> shufflePositions(List<Integer> data) { 
    Collections.shuffle(data); 
    return data; 
} 

public void solve() { 
    List<Integer> data = new ArrayList<>(); 
    data.add(12); 
    data.add(13); 
    data.add(17); 
    data.add(18); 
    data.add(19); 
    data.add(11); 
    data.add(20); 
    List<Integer> result = new ArrayList<>(); 
    result.addAll(data); 
    result.addAll(shufflePositions(new ArrayList<>(data))); 
    result.addAll(shufflePositions(new ArrayList<>(data))); 
} 
1

试试这个:

List<Integer> integers = new ArrayList<>(Arrays.asList(12, 13, 17, 18, 19, 11, 20)); 

for(int i=0; i<2; i++) { 

    List<Integer> IntegersClone = new ArrayList<>(integers); 

    Collections.shuffle(IntegersClone); 

    integers.addAll(IntegersClone); 
} 

//Output : [12, 13, 17, 18, 19, 11, 20, 19, 17, 12, 11, 20, 18, 13, 20, 12, 19, 11, 18, 13, 13, 11, 17, 18, 19, 12, 17, 20] 
System.out.print(integers);