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;
}
java打乱ArrayList随机
Java方法:检查ArrayList是否为空并打乱元素顺序
最新推荐文章于 2024-06-02 01:38:52 发布
这段代码包含两个Java方法。第一个方法`isEmpty`检查给定的ArrayList是否为空或其大小为0。第二个方法`randomList`用于打乱输入ArrayList的元素顺序,它通过创建一个新的ArrayList并将源列表中的元素随机移除并添加到新列表中来实现。如果源列表为空,`randomList`方法将原样返回。
1810

被折叠的 条评论
为什么被折叠?



