java打乱ArrayList生成一个随机ArrayList列表,javaarraylist
分享于 点击 18695 次 点评:61
java打乱ArrayList生成一个随机ArrayList列表,javaarraylist
自己写了一个,有时候会有需要。
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;
}
相关文章
- 暂无相关文章
用户点评