第一题
public class HomeWork01 {
// 1、产生10个1-100的随机数,并放到一个数组中,
// 把数组中大于等于10的数字放到一个list集合中,并打印到控制台
public static void main(String[] args) {
int[] arr = new int[10];
List<Integer> list = new ArrayList();
for (int i = 0; i < arr.length; i++) {
arr[i] = (int) (Math.random() * 99 + 1);
if (arr[i] >= 10) {
list.add(arr[i]);
}
}
System.out.println(Arrays.toString(arr));
System.out.println("list集合"+list);
}
}
第二题
public class HomeWork02 {
// 2、已知数组存放一批QQ号码,QQ号码最长为11位,最短为5位
// String[] strs ={"12345","67891","12347809933","98765432102","67891","12347809933"}。
// 将该数组里面的所有qq号都存放在LinkedList中,将list中重复元素删除,
// 将list中所有元素分别用迭代器和增强for循环打印出来
public static void main(String[] args) {
String[] strs = {"12345", "67891", "12347809933", "98765432102", "67891", "12347809933"};
Set<String> set = new HashSet<>();//元素无序且不重复
for (String num : strs) {
set.add(num);
}
System.out.println("删除后集合:" + set);
LinkedList<String> list = new LinkedList<>();
Iterator<String> it = set.iterator();
while (it.hasNext()) {
list.add(it.next());
}
System.out.println("迭代器的方式:"+list);
System.out.println("增强for循环的方式:");
for (String str : list){
System.out.print(str+" ");
}
}
}
第三题
public class HomeWork03 {
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);
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;
@Override
public int compareTo(Student o) {
if (this.score == o.score) {
return this.age - o.age;
}
return o.score - this.score > 0 ? 1 : -1;
}
public Student() {
}
public Student(String name, int age, float score) {
this.name = name;
this.age = age;
this.score = score;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", score=" + score +
'}';
}
}
第四题
public class HomeWork04 {
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<Integer, String>();
map.put(1, "张三丰");
map.put(2, "周芷若");
map.put(3, "汪峰");
map.put(4, "灭绝师太");
//entrySet()创建键值对集合
Set<Map.Entry<Integer, String>> entries = map.entrySet();
Iterator<Map.Entry<Integer, String>> it = entries.iterator();
while (it.hasNext()){
Map.Entry<Integer, String> next = it.next();
Integer key = next.getKey();
String value = next.getValue();
System.out.println(key+":"+value);
}
map.put(5, "李晓红");
map.remove(1);
map.replace(2, "周芷若", "张琳");
System.out.println(entries);
}
}
第五题
public class HW05 {
public static void main(String[] args) {
String[] arr = {"科特迪瓦", "阿根廷", "澳大利亚", "塞尔维亚",
"荷兰", "尼日利亚", "日本", "美国", "中国", "新西兰",
"巴西", "比利时", "韩国", "喀麦隆", "洪都拉斯", "意大利"};
List<Object> list = new ArrayList<>();
for (int i = 0; i < arr.length; i++) {
list.add(arr[i]);
}
Collections.shuffle(list);
System.out.println("随机排序:"+list);
List<Object> list1 = list.subList(0, 4);
List<Object> list2 = list.subList(4, 8);
List<Object> list3 = list.subList(8, 12);
List<Object> list4 = list.subList(12, 16);
System.out.println("第一队:"+list1);
System.out.println("第二队:"+list2);
System.out.println("第三队:"+list3);
System.out.println("第四队:"+list4);
}
}