- 向list集合添加姓名{张三李四,王五二丫,钱六,孙七},将二丫替换为王小丫。
- AmayList集合中有如下内容: {33,11,77,55}, 使用Collections sort()对AnmayList 集合中的数据进行排序,并打印出排序后的结果。
- 成绩排序
需求:用TreeSet存储多个学生信息(姓名, 语文成绩,数学成绩),并遍历该集合
要求:按照总分从高到低出现。
- 需求:创建一个存储学生对象的集合,存储多个学生对象,使用程序实现在控制台遍历该集合
要求:学生对象的成员变量值相同,我们就认为是同一个对象
| 1、 |
| package com.xxx; |
| |
| import java.util.ArrayList; |
| import java.util.List; |
| |
| public class Test1 { |
| public static void main(String[] args) { |
| List list = new ArrayList<>(); |
| |
| list.add("张三"); |
| list.add("李四"); |
| list.add("王五"); |
| list.add("二丫"); |
| list.add("钱六"); |
| list.add("孙七"); |
| |
| int index = list.indexOf("二丫"); |
| list.set(index, "王二丫"); |
| System.out.println(list); |
| } |
| } |
| 2、 |
| package com.xxx; |
| |
| import java.util.ArrayList; |
| import java.util.Collections; |
| import java.util.List; |
| |
| public class Test02 { |
| public static void main(String[] args) { |
| List list = new ArrayList(); |
| list.add(33); |
| list.add(11); |
| list.add(77); |
| list.add(55); |
| |
| Collections.sort(list); |
| |
| System.out.println(list); |
| } |
| } |
| 3、 |
| package com.xxx; |
| |
| public class Students { |
| private String name; |
| private double chineseScore; |
| private double mathScore; |
| |
| public Students() { |
| } |
| |
| public Students(String name, double chineseScore, double mathScore) { |
| this.name = name; |
| this.chineseScore = chineseScore; |
| this.mathScore = mathScore; |
| } |
| |
| public String getName() { |
| return name; |
| } |
| |
| public void setName(String name) { |
| this.name = name; |
| } |
| |
| public double getChineseScore() { |
| return chineseScore; |
| } |
| |
| public void setChineseScore(double chineseScore) { |
| this.chineseScore = chineseScore; |
| } |
| |
| public double getMathScore() { |
| return mathScore; |
| } |
| |
| public void setMathScore(double mathScore) { |
| this.mathScore = mathScore; |
| } |
| |
| @Override |
| public String toString() { |
| return "Students{" + |
| "name='" + name + '\'' + |
| ", chineseScore=" + chineseScore + |
| ", mathScore=" + mathScore + |
| '}'; |
| } |
| } |
| package com.xxx; |
| |
| import java.util.Comparator; |
| import java.util.TreeSet; |
| |
| public class Test03 { |
| public static void main(String[] args) { |
| |
| TreeSet treeSet = new TreeSet(new Comparator() { |
| @Override |
| public int compare(Object o1, Object o2) { |
| Students s1 = (Students) o1; |
| Students s2 = (Students) o2; |
| return (int) ((s2.getChineseScore() + s2.getMathScore()) - (s1.getChineseScore()+s1.getMathScore())); |
| } |
| }); |
| |
| treeSet.add(new Students("路飞", 10, 20)); |
| treeSet.add(new Students("索隆", 80, 70)); |
| treeSet.add(new Students("山治", 90, 80)); |
| treeSet.add(new Students("甚平", 70, 90)); |
| treeSet.add(new Students("罗宾", 100, 100)); |
| |
| System.out.println(treeSet); |
| } |
| } |
| 4、 |
| package com.xxx; |
| |
| import java.util.Objects; |
| |
| public class Student { |
| private String name; |
| private int age; |
| |
| public Student() { |
| } |
| |
| public Student(String name, int age) { |
| this.name = name; |
| this.age = age; |
| } |
| |
| 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; |
| } |
| |
| @Override |
| public boolean equals(Object o) { |
| if (this == o) return true; |
| if (o == null || getClass() != o.getClass()) return false; |
| Student student = (Student) o; |
| return age == student.age && |
| Objects.equals(name, student.name); |
| } |
| |
| @Override |
| public int hashCode() { |
| return Objects.hash(name, age); |
| } |
| |
| @Override |
| public String toString() { |
| return "Student{" + |
| "name='" + name + '\'' + |
| ", age=" + age + |
| '}'; |
| } |
| } |
| package com.xxx; |
| |
| import java.util.HashSet; |
| import java.util.Set; |
| |
| public class Test4 { |
| public static void main(String[] args) { |
| Set set = new HashSet<Student>(); |
| |
| Student stu1 = new Student("伏黑", 18); |
| Student stu2 = new Student("虎杖", 17); |
| Student stu3 = new Student("钉崎", 19); |
| Student stu4 = new Student("钉崎", 19); |
| |
| set.add(stu1); |
| set.add(stu2); |
| set.add(stu3); |
| set.add(stu4); |
| |
| for (Object o : set) { |
| System.out.println(o); |
| } |
| } |
| } |
| |
本文作者:Ritchie里其
本文链接:https://www.cnblogs.com/wang-zeyu/p/16789671.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步