匿名内部类comparator
时间: 2023-08-29 09:11:06 浏览: 102
匿名内部类comparator的代码如下:
```
new Comparator<Student>() {
public int compare(Student a, Student b) {
if (a.age > b.age)
return 1;
else if (a.age == b.age)
return 0;
else
return -1;
}
}
```
这段代码创建了一个匿名内部类,实现了Comparator接口,并重写了compare方法。在compare方法中,根据Student对象的age属性进行比较,如果a的age大于b的age,则返回1,如果a的age等于b的age,则返回0,如果a的age小于b的age,则返回-1。这样,通过这个匿名内部类作为Comparator的实例,可以实现按照age升序排序Student对象数组。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* *3* [【PTA】匿名内部类:实现Comparator比较器](https://blog.csdn.net/qq_63806300/article/details/127928435)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"]
[ .reference_list ]
阅读全文