一个简单的冒泡排序

 

 1 public class JDBC {
 2     public static void main(String[]args){
 3         Student stu  = new Student("张三",1,80);
 4         Student stu1  = new Student("李四",2,60);
 5         Student stu2  = new Student("王五",3,77);
 6         Student stu3  = new Student("赵六",4,90);
 7         ArrayList<Student> list = new ArrayList<>();
 8         list.add(stu);
 9         list.add(stu1);
10         list.add(stu2);
11         list.add(stu3);
12 
13         //冒泡排序
14         for (int i = 0; i < list.size()-1; i++) {
15             for (int j = 0; j < list.size()-1-i; j++) {
16                 //建立对象接收集合中的Student
17                 Student s = list.get(j);
18                 Student s1 = list.get(j+1);
19                 //比较成绩
20                 if(s.getScore()>s1.getScore()){
21                     list.set(j, s1);
22                     list.set(j+1, s);
23                 }
24             }
25         }
26         for(Student st : list){
27             System.out.println(st);
28         }
29     }
30 }
31 
32 class Student{
33     private String name;
34     private int id;
35     private int score;
36     public String getName() {
37         return name;
38     }
39     public void setName(String name) {
40         this.name = name;
41     }
42     public int getId() {
43         return id;
44     }
45     public void setId(int id) {
46         this.id = id;
47     }
48     public int getScore() {
49         return score;
50     }
51     public void setScore(int score) {
52         this.score = score;
53     }
54     public Student(String name, int id, int score) {
55         super();
56         this.name = name;
57         this.id = id;
58         this.score = score;
59     }
60     public Student() {
61         super();
62         // TODO Auto-generated constructor stub
63     }
64     @Override
65     public String toString() {
66         return "Student [name=" + name + ", id=" + id + ", score=" + score + "]";
67     }
68 
69 }

执行结果:

 

转载于:https://www.cnblogs.com/datacenter/p/11528286.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值