前言
在做試卷的時候,需要將一個句子中的單詞、一個單詞中的字符、選擇題中的答題項打亂生成一個隨機的序列,下面我將其抽象成工具類,方便大家以后復用。
示例代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
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提供方便。如果有疑問可以留言交流。