• 一个简单的冒泡排序


     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 }

    执行结果:

  • 相关阅读:
    2019年9月15日晚间测试-T1
    机房巨佬的随机名称生成器
    初来乍到
    GKurumi记
    GKurumi记
    小P的团战
    什么才算是真正的编程能力?
    java冒泡排序和快速排序
    “转行做程序员”很难?这里有4个重要建议
    Linux文件I/O(一)
  • 原文地址:https://www.cnblogs.com/datacenter/p/11528286.html
Copyright © 2020-2023  润新知