@[TOC] Map、List、对象、以及数组的应用
1、产生10个1-100的随机数,并放到一个数组中,把数组中大于等于10的数字放到一个list集合中,并打印到控制台。
import java.util.ArrayList;
import java.util.List;
public class HWDemo01 {
public static void main(String[] args) {
int[] randomNums = new int[10];
List<Integer> list = new ArrayList<>();
for (int i = 0; i < randomNums.length; i++) {
randomNums[i] = (int) (Math.random() * 99 + 1);
if (randomNums[i] >= 10) {
list.add(randomNums[i]);
}
}
System.out.println(list);
}
}
2、已知数组存放一批QQ号码,QQ号码最长为11位,最短为5位
String[] strs ={“12345”,“67891”,“12347809933”,“98765432102”,“67891”,“12347809933”}。
将该数组里面的所有qq号都存放在LinkedList中,将list中重复元素删除,
将list中所有元素分别用迭代器和增强for循环打印出来。
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
public class HWDemo02 {
public static void main(String[] args) {
String[] strs = {"12345", "67891", "12345", "12347809933",
"67891", "98765432102", "67891", "12347809933"};
List<String> list = new LinkedList<>();
for (int i = 0; i < strs.length; i++) {
list.add(strs[i]);
}
System.out.println(list);
Iterator<String> listIt = list.iterator();
List<String> l = new LinkedList<>();
int index = 0;
while (listIt.hasNext()) {
l.clear();
l.add(listIt.next());
if (list.indexOf(l.get(0)) != index) {
listIt.remove();
index--;
}
index++;
}
//迭代器打印输出元素
listIt = list.iterator();
System.out.print("迭代器:[");
while (listIt.hasNext()) {
System.out.print(listIt.next());
if (listIt.hasNext()) {
System.out.print(", ");
}
}
System.out.println("]");
//增强For循环打印输出元素
System.out.print("增强For循环:[");
int indexNum = 0;
for (String str : list) {
System.out.print(str);
indexNum++;
if (indexNum == list.size()) {
System.out.print("]");
} else {
System.out.print(", ");
}
}
}
}
3、用Comparable接口对下列四位同学的成绩做降序排序,如果成绩一样,那在成绩排序的基础上按照年龄由小到大排序。 提示:编写一个Student类用来实现Comparable接口,并在其中重写CompareTo(Student o)方法
public class HWDemo03 {
public static void main(String[] args) {
Student stu1 = new Student("liusan", 20, 90.0F);
Student stu2 = new Student("lisi", 22, 90.0F);
Student stu3 = new Student("wangwu", 20, 99.0F);
Student stu4 = new Student("sunliu", 22, 100.0F);
Student[] stus = {stu1, stu2, stu3, stu4};
System.out.println("排序前:");
for (Student student : stus) {
System.out.println(student);
}
Student s;
for (int i = 0; i < stus.length; i++) {
for (int j = 0; j < i - 1; j++) {
if (stus[i].compareTo(stus[j]) == 1) {
s = stus[i];
stus[i] = stus[j];
stus[j] = s;
}
}
}
System.out.println("排序后:");
for (Student student : stus) {
System.out.println(student);
}
}
}
class Student implements Comparable {
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(Object o) {
int isMax;
if (!(o instanceof Student) && o == null) {
throw new RuntimeException();
}
Student s = (Student) o;
if (this.getScore() > s.getScore()) {
isMax = 1;
} else if (this.getScore() == s.getScore()) {
if (this.getAge() <= s.getAge()) {
isMax = 1;
} else {
isMax = -1;
}
} else {
isMax = -1;
}
return isMax;
}
@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集合如下:
Map<Integer,String> map = new HashMap<Integer, String>();
map.put(1, “张三丰”);map.put(2, “周芷若”);map.put(3, “汪峰”);map.put(4, “灭绝师太”);
要求:
1.遍历集合,并将序号与对应人名打印。
2.向该map集合中插入一个编码为5姓名为李晓红的信息
3.移除该map中的编号为1的信息
4.将map集合中编号为2的姓名信息修改为"周林"
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class HWDemo04 {
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<>();
map.put(1, "张三丰");
map.put(2, "周芷若");
map.put(3, "汪峰");
map.put(4, "灭绝师太");
Set<Map.Entry<Integer, String>> entries = map.entrySet();
Iterator<Map.Entry<Integer, String>> entryIt = entries.iterator();
while (entryIt.hasNext()) {
System.out.println(entryIt.next());
}
System.out.println("=======================");
map.put(5, "李晓红");
map.remove(1);
map.put(2, "周林");
entryIt = entries.iterator();
while (entryIt.hasNext()) {
System.out.println(entryIt.next());
}
}
}
5、已知有十六支男子足球队参加2020 奥运会。写一个程序,把这16 支球队随机分为4 个组。
采用List集合和随机数
2020 奥运会男足参赛国家:
科特迪瓦,阿根廷,澳大利亚,塞尔维亚,荷兰,尼日利亚、日本,美国,中国,新西兰,巴西,比利时,韩国,喀麦隆,洪都拉斯,意大利
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class HWDemo05 {
public static void main(String[] args) {
String[] strs = {"科特迪瓦", "阿根廷", "澳大利亚", "塞尔维亚",
"荷兰", "尼日利亚", "日本", "美国", "中国",
"新西兰", "巴西", "比利时", "韩国", "喀麦隆",
"洪都拉斯", "意大利"};
int group = 4;//小组数
int[] randomIndex = new int[strs.length];
for (int i = 0; i < randomIndex.length; i++) {
randomIndex[i] = (int) (Math.random() * randomIndex.length);
for (int j = 0; j < i; j++) {
if (randomIndex[i] == randomIndex[j]) {
i--;
break;
}
}
}
System.out.println(Arrays.toString(randomIndex));
List<String> list1 = new ArrayList<>();
List<String> list2 = new ArrayList<>();
List<String> list3 = new ArrayList<>();
List<String> list4 = new ArrayList<>();
for (int i = 0; i < randomIndex.length; i++) {
if (i < group) {
list1.add(strs[randomIndex[i]]);
} else if (i < group * 2) {
list2.add(strs[randomIndex[i]]);
} else if (i < group * 3) {
list3.add(strs[randomIndex[i]]);
} else {
list4.add(strs[randomIndex[i]]);
}
}
System.out.println("第1队:" + list1 + "\n第2队:" +
list2 + "\n第3队:" + list3 + "\n第4队:" + list4);
}
}
————————————————
版权声明:本文中的题目为CSDN博主「排骨玉米汤」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_44796239/article/details/123692848