java打乱ArrayList生成一个随机ArrayList列表

来源:互联网 发布:上海浦东机场有没有mac 编辑:程序博客网 时间:2024/06/01 18:28

自己写了一个,有时候会有需要。

public static <V> boolean isEmpty(ArrayList<V> sourceList) {        return (sourceList == null || sourceList.size() == 0);    }

/**     * 打乱ArrayList     *      * */    public static <V> ArrayList<V> randomList(ArrayList<V> sourceList){    if (isEmpty(sourceList)) {            return sourceList;        }        ArrayList<V> randomList = new ArrayList<V>( sourceList.size( ) );    do{    int randomIndex = Math.abs( new Random( ).nextInt( sourceList.size() ) );        randomList.add( sourceList.remove( randomIndex ) );    }while( sourceList.size( ) > 0 );        return randomList;    }


1 0
原创粉丝点击