- package demo;
- import java.util.Comparator;
- import java.util.Set;
- import java.util.TreeSet;
- /*
- * 现有Student类,属性有name, age, score(int类型).
- 要求 : 按照学生的分数排序, 分数大的童鞋在前面, 如果分数相同, 那么年龄小的在前面, 如果分数年龄都相同,
- 则按照姓名(英文的即可)的字典顺序排序.要求:不允许在描述类上写排序规则.
- 下面是给大家学生.
- Student("Tom",24, 89)
- Student("Robin",32, 99));
- Student("Jerry",24, 99));
- Student("Lili",23, 87));
- Student("Jack",22, 87));
- Student("LiLei",25, 95));
- Student("Robin",32 ,99));
- * */
- public class Demo18 {
- public static void main(String[] args) {
-
- Set<Student> set = new TreeSet<Student>(new MyCop());
- set.add(new Student("Tom",24, 89));
- set.add(new Student("Robin",32, 99));
- set.add(new Student("Jerry",24, 99));
- set.add(new Student("Lili",23, 87));
- set.add(new Student("Jack",22, 87));
- set.add(new Student("Robin",32 ,99));
- set.add(new Student("LiLei",25, 95));
-
- for(Student s : set){
-
- System.out.println("姓名:"+s.getName()+",年龄:"+s.getAge()+",评分:"+s.getScore());
- }
-
- }
- }
- class Student {
-
- private String name;
- private int age;
- private int score;
- public Student() {
- super();
- }
- public Student(String name, int age, int score) {
- super();
- this.name = name;
- this.age = age;
- this.score = score;
- }
- 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;
- }
- public int getScore() {
- return score;
- }
- public void setScore(int score) {
- this.score = score;
- }
- }
- class MyCop implements Comparator<Student> {
- @Override
- public int compare(Student o1, Student o2) {
-
- int n = new Integer(o1.getScore()).compareTo(new Integer(o2.getScore()));
-
- if(n == 0){
- int i = new Integer(o1.getAge()).compareTo(new Integer(o2.getAge()));
- if(i == 0){
- return o1.getName().compareTo(o2.getName());
- }else{
- return i;
- }
- }else if(n <= 0){
- return 1;
- }else{
- return -1;
- }
- }
-
-
- }attach://73059.jpg
复制代码
- package demo;
- /*
- * 写一个延迟加载的单例模式的程序。
- * */
- public class Demo19 {
- private Demo19(){}
- private static Demo19 d = null;
- public static Demo19 getInstance(){
- if(d == null){
- return d = new Demo19();
- }else{
- return d;
- }
- }
- }
复制代码
- package demo;
- import java.io.BufferedWriter;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.util.Set;
- import java.util.TreeSet;
- /*
- * 把以下IP存入一个txt文件,编写程序把这些IP按数值大小,从小到达排序并打印出来。
- 61.54.231.245
- 61.54.231.9
- 61.54.231.246
- 61.54.231.48
- 61.53.231.249
- * */
- public class Demo20 {
- public static void main(String[] args) {
-
- try {
- BufferedWriter buffw = new BufferedWriter(new FileWriter("d:/IP.txt"));
- Set<String> set = new TreeSet<String>();
- set.add("61.54.231.245");
- set.add("61.54.231.9");
- set.add("61.54.231.246");
- set.add("61.54.231.48");
- set.add("61.53.231.249");
-
- for(String s : set){
-
- buffw.write(s);
- buffw.newLine();
- buffw.flush();
- }
- buffw.close();
-
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
复制代码
http://bbs.itheima.com/forum.php?mod=attachment&aid=NzMwNjF8OGQ5ODkyYWIzNDgzN2IwMTFiMDUwMGZiNDA1YjY4MzV8MTczNDkzMjY5Mg%3D%3D&request=yes&_f=.jpg |
-
1.jpg
(78.97 KB, 下载次数: 68)
-
2.jpg
(30.18 KB, 下载次数: 38)
|