konstantin
konstantin

Reputation: 893

Duplicate and Randomize the position of items within a list

I have got an Arraylist of integers in java called list which contains 7 elements [12, 13, 17, 18, 19, 11, 20]. I want to copy the same elements two times in the end of the list with random order, so in the end to have the same integers three times (so in total 21 items), however, with random order for every 7 items. How can i do so in Java?

Upvotes: 0

Views: 73

Answers (5)

Christian
Christian

Reputation: 22343

Just copy your list two times and shuffle it:

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

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

Upvotes: 1

Mouad EL Fakir
Mouad EL Fakir

Reputation: 3749

Try this :

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);

Upvotes: 1

aUserHimself
aUserHimself

Reputation: 1597

Try using Collections.shuffle() and call it twice for the initial data list:

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)));
}

Upvotes: 1

rakwaht
rakwaht

Reputation: 3967

// 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);

Upvotes: 1

stealthjong
stealthjong

Reputation: 11093

Just copy the list and shuffle for each iteration.

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);
}

Upvotes: 1

Related Questions