时间:2022-06-05 11:18:53 | 栏目:JAVA代码 | 点击:次
前言
在做试卷的时候,需要将一个句子中的单词、一个单词中的字符、选择题中的答题项打乱生成一个随机的序列,下面我将其抽象成工具类,方便大家以后复用。
示例代码
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提供方便。如果有疑问可以留言交流。