Java ArrayList Shuffle shuffle(ArrayList population, int sample)

Here you can find the source of shuffle(ArrayList population, int sample)

Description

shuffle

License

Apache License

Declaration

public static <T> ArrayList<T> shuffle(ArrayList<T> population, int sample) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.util.ArrayList;
import java.util.Collections;

public class Main {
    public static <T> ArrayList<T> shuffle(ArrayList<T> population, int sample) {
        ArrayList<T> newList = new ArrayList<T>();
        ArrayList<T> ret = new ArrayList<T>();
        newList.addAll(population);/*from w  w  w  . j a  va2 s. c om*/

        Collections.shuffle(newList);
        ret.addAll(newList);
        for (int i = sample; i < ret.size(); i++) {
            ret.remove(i);
            i--;
        }
        newList.clear();
        return ret;
    }
}

Related

  1. shuffle(ArrayList l)