package edu.del;

 import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner; class Student implements Comparable<Student>{
String name;
int score; public Student(String name, int score) {
this.name = name;
this.score = score;
} public Student() { } public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getScore() {
return score;
} public void setScore(int score) {
this.score = score;
} @Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", score=" + score +
'}';
} // @Override
// public int compareTo(Student o) {
// //假如result返回1。Collections.sort(List)方法就是升序;
// //假如result返回-1。Collections.sort(List)方法就是降序;
// int num=new Integer(o.getScore()).compareTo(this.getScore());//这里面是按照降序排列
// //int num=new Integer(this.getScore()).compareTo(this.getScore()); //测试升序
// return num;
//
// }
@Override
public int compareTo(Student o) {
//这里是按照名字字符属性排序
int num =o.getName().compareTo(this.getName());
//int num=this.getName().compareTo(o.getName());
return num;
}
} public class sort_class {
public static void main(String[] args) {
Scanner scanner =new Scanner(System.in);
System.out.println("请输入学生数量:");
int num =scanner.nextInt();
Student[] student =new Student[num];
List<Student> list =new ArrayList<>(); for (int i = 0; i <num ; i++) { list.add(new Student(scanner.next(),scanner.nextInt()));
}
Collections.sort(list); //输出一下看看 for (int i = 0; i <num ; i++) {
System.out.print(list.get(i).getName()+" ");
System.out.println(list.get(i).score);
}
}
}

最新文章

  1. JavaScript分离代码理解
  2. linux一些常用配置
  3. 使用jasperreports-5.6.0.jar导致的问题
  4. 字符流和字节流(FileReader类和FileWriter类)
  5. Python之路【第九篇】:Python操作 RabbitMQ、Redis、Memcache、SQLAlchemy
  6. BAT-使用BAT方法清理系统垃圾
  7. 第一章 USB Type C的基本原理
  8. cocos2d-x-lua基础系列教程三(lua面向对象)
  9. Delphi基本图像处理方法汇总
  10. .net 笔试题目
  11. PADS Layout VX.2.3 出Gerber文件时遇到一个奇怪的现象
  12. LeetCode算法题-Subtree of Another Tree(Java实现)
  13. Jquery遍历之获取子级元素、同级元素和父级元素
  14. 两个时间点计算相隔几年,几个月,几天-java
  15. Golang——垃圾回收GC(2)
  16. set集合遍历
  17. Swift5 语言指南(二十) 类型转换
  18. MySQL Disk--磁盘相关参数
  19. [翻译] VENCalculatorInputView
  20. TeamWork#1,Week 5,Suggestions for Team Project

热门文章

  1. vue部署的路径问题
  2. linux 复制文件
  3. 个人信息——头像更换(拍照或相册上传)~ 微信小程序
  4. springcloud-feign组件实现声明式的调用
  5. vue-cli webpack配置 简单分析
  6. DHCP协议分析(Wireshark)
  7. linux jpg文件查找木马
  8. python day1 之三级菜单的正确姿势
  9. CSS3-1
  10. 物联网智能家居ppt