正在回答
4回答
同学你好,老师这边测试同学编写的代码是没有问题,代码也没有报错,具体如下:
建议同学可以尝试将重新创建项目将代码粘贴过去,尝试运行一下,或者重新启动一下idea哦~
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
Ctrlzhao
2019-10-25 11:52:49
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | public class Cat { private String name; private int month; private String species; public Cat() { } public Cat(String name, int month, String species) { this .setName(name); this .setMonth(month); this .setSpecies(species); } public String getName() { return name; } public void setName(String name) { this .name = name; } public int getMonth() { return month; } public void setMonth( int month) { this .month = month; } public String getSpecies() { return species; } public void setSpecies(String species) { this .species = species; } @Override public String toString() { return "[名字:" + name + "\t年龄:" + month + "\t品种;" + species+ "]" ; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 | import java.util.Comparator; public class NameComparator implements Comparator<Cat> { @Override public int compare(Cat o1, Cat o2) { //按名字升序排序 String name1 = o1.getName(); String name2 = o2.getName(); int n = name1.compareTo(name2); return n; } } |
Ctrlzhao
2019-10-24 22:50:53
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | package vip.imooc.sort; import java.util.ArrayList; import java.util.Collections; import java.util.List; /** * @author ---zhao * @date 2019/10/24-22:13 */ public class CatTest { public static void main(String[] args){ Cat huahua= new Cat( "花花" , 5 , "辉夜大小姐的猫" ); Cat fanfan= new Cat( "饭饭" , 3 , "承太郎的猫" ); Cat kongkong= new Cat( "空空" , 7 , "藤原书记的小猫" ); List<Cat> catList= new ArrayList<Cat>(); catList.add(huahua); catList.add(fanfan); catList.add(kongkong); //排序前 System.out.println( "排序前:" ); for (Cat cat:catList){ System.out.println(cat); } System.out.println( "排序后:" ); Collections.sort(catList, new NameComparator()); for (Cat cat:catList){ System.out.println(cat); } } } |
1. Java 零基础入门
- 参与学习 人
- 提交作业 3802 份
- 解答问题 11489 个
本阶段带你迈入Java世界,学习Java必备基础知识,基础语法、面向对象思想以及常用工具类的使用。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧