Hello friend, I'm |

Ritchie里其

园龄:2年6个月粉丝:4关注:7

练习题03List

  1. 向list集合添加姓名{张三李四,王五二丫,钱六,孙七},将二丫替换为王小丫。
  2. AmayList集合中有如下内容: {33,11,77,55}, 使用Collections sort()对AnmayList 集合中的数据进行排序,并打印出排序后的结果。
  3. 成绩排序
    需求:用TreeSet存储多个学生信息(姓名, 语文成绩,数学成绩),并遍历该集合
    要求:按照总分从高到低出现。
  4. 需求:创建一个存储学生对象的集合,存储多个学生对象,使用程序实现在控制台遍历该集合
    要求:学生对象的成员变量值相同,我们就认为是同一个对象
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 中国大陆许可协议进行许可。

posted @   Ritchie里其  阅读(70)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
✨欢迎你~🍻
✨欢迎你~🍻
评论
收藏
关注
推荐
深色
回顶
收起
  1. 1 遥か Aimer
遥か - Aimer
00:00 / 00:00
An audio error has occurred.

作词 : aimerrhythm/田中ユウスケ

作曲 : 田中ユウスケ

编曲 : 玉井健二/百田留衣

海岸線の雨に ちらばった君の影

思い出が交差する 海辺の街

君はあの日のまま いまも夢を見てた

君はあの日のまま いまも夢を見てた

遥か記憶の空 2人照らす光

遥か記憶の空 2人照らす光

膝までの浅瀬で 見つけた星

君まで届くなんてさ ありえないような

浅い眠りの中で 深い夢から覚めて

浅い眠りの中で 深い夢から覚めて

裸足のまま駆けてく まばゆい星

君はあの日のまま どんな夢を見てた?

君はあの日のまま どんな夢を見てた?

遥か記憶の空 2人照らす光

遥か記憶の空 2人照らす光

いつまでもこうして 笑っててほしい

夜空に舞い上がる 幾千の花びら

でたらめな誓いで 生きてく日々

君から届くなんてさ ありえないような