2017-02-16 93 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);