1、产生10个1-100的随机数,并放到一个数组中, 把数组中大于等于10的数字放到一个list集合中,并打印到控制台。
public static void main(String[] args) {
int[] arrs = new int[10];
List list = new ArrayList();
for (int i = 0; i < arrs.length; i++) {
arrs[i] =(int) (Math.random() * 100 + 1);
System.out.print(arrs[i] + " ");
if (arrs[i] >= 10){
list.add(arrs[i]);
}
}
System.out.println(list);
}
2、已知数组存放一批QQ号码,QQ号码最长为11位,最短为5位 String[] strs ={"12345","67891","12347809933","98765432102","67891","12347809933"}。 将该数组里面的所有qq号都存放在LinkedList中,将list中重复元素删除, 将list中所有元素分别用迭代器和增强for循环打印出来。
String[] strs ={"12345","67891","12347809933","98765432102","67891","12347809933"};
//List 有序可以重复 Set 无序不可以重复
Set<String> set = new HashSet<>();
for (int i = 0; i < strs.length; i++) {
set.add(strs[i]);
}
System.out.println(set);
List<String> list = new LinkedList<>();
Iterator<String> it = set.iterator();
while (it.hasNext()){
list.add(it.next());
}
System.out.println(list);
for (String str : list){
System.out.println(str);
}
3、用Comparable接口对下列四位同学的成绩做降序排序,如果成绩一样,那在成绩排序的基础上按照年龄由小到大排序。 提示: 编写一个Student类用来实现Comparable<Student>接口,并在其中重写CompareTo(Student o)方法
Student stu1 = new Student("liusan", 20, 90F);
Student stu2 = new Student("lisi", 22, 90F);
Student stu3 = new Student("wangwu", 20, 99F);
Student stu4 = new Student("sunliu", 22, 100F);
List<Student> list = new ArrayList<>();
list.add(stu1);
list.add(stu2);
list.add(stu3);
list.add(stu4);
System.out.println(list);
//
Collections.sort(list);
System.out.println(list);
}
}
class Student implements Comparable<Student>{
private String name;
private int age;
private float score;
public Student(String name, int age, float score) {
this.name = name;
this.age = age;
this.score = score;
}
@Override
public int compareTo(Student o) {
if (this.score == o.score){//成绩一样
return this.age - o.age;//按年龄从小到大排
}
return o.score - this.score> 0 ? 1 : -1;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", score=" + score +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public float getScore() {
return score;
}
public void setScore(float score) {
this.score = score;
}
4.
Map<Integer,String> map = new HashMap<Integer,String>();
map.put(1,"张三丰");
map.put(2,"周芷若");
map.put(3,"汪峰");
map.put(4,"灭绝师太");
System.out.println(map);
Set<Integer> keys = map.keySet();
Iterator<Integer> itKey = keys.iterator();
while(itKey.hasNext()){
Integer key = itKey.next();
String value = map.get(key);
System.out.println(key + ":" +value);
}
map.put(5,"李晓红");
map.remove(1);
System.out.println(map);
map.put(2,"周林");
System.out.println(map);
5、已知有十六支男子足球队参加2020 奥运会。写一个程序,把这16 支球队随机分为4 个组。 采用List集合和随机数 2020 奥运会男足参赛国家: 科特迪瓦,阿根廷,澳大利亚,塞尔维亚,荷兰,尼日利亚、日本,美国,中国, 新西 兰,巴西,比利时,韩国,喀麦隆,洪都拉斯,意大利
List<String> list = new ArrayList<>();
list.add("科特迪瓦");
list.add("阿根廷");
list.add("澳大利亚");
list.add("塞尔维亚");
list.add("荷兰");
list.add("尼日利亚");
list.add("日本");
list.add("美国");
list.add("中国");
list.add("新西兰");
list.add("巴西");
list.add("比利时");
list.add("韩国");
list.add("喀麦隆");
list.add("洪都拉斯");
list.add("意大利");
Random random = new Random();
String str;
for (int i = 0; i < 4; i++) {
System.out.println((i+1) + "组");
for (int j = 0; j < 4; j++) {
str = list.get(random.nextInt(list.size()));
System.out.print(" " + str);
list.remove(str);
}
System.out.println("\n");
}
}