-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomSelect.java
More file actions
34 lines (32 loc) · 939 Bytes
/
Copy pathRandomSelect.java
File metadata and controls
34 lines (32 loc) · 939 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package array;
import java.util.ArrayList;
import java.util.Random;
public class RandomSelect {
ArrayList<String> group=new ArrayList<>();
void addMember(String member){
if(group.contains(member))
return;
group.add(member);
}
String select(){
String result=null;
if(!group.isEmpty()){
int randomNum=new Random().nextInt(group.size());
result=group.get(randomNum);
}
return result;
}
public static void main(String[] args) {
RandomSelect selector=new RandomSelect();
selector.addMember("Jim");
selector.addMember("bill");
selector.addMember("tom");
selector.addMember("sam");
selector.addMember("peter");
System.out.println("select:"+selector.select());
System.out.println("select:"+selector.select());
System.out.println("select:"+selector.select());
System.out.println("select:"+selector.select());
System.out.println("select:"+selector.select());
}
}