java打亂ArrayList生成一個隨機ArrayList列表
阿新 • • 發佈:2019-01-02
自己寫了一個,有時候會有需要。
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; }