前言
在做試卷的時候,需要將一個句子中的單詞、一個單詞中的字符、選擇題中的答題項打亂生成一個隨機的序列,下面我將其抽象成工具類,方便大家以后復用。
示例代碼
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提供方便。如果有疑問可以留言交流。
新聞熱點
疑難解答