JAVA基礎之物件的排序
基本資料型別的排序
在JAVA語言中,對於基本的資料型別組成的陣列或者集合,都可以使用sort()方法進行排序。因為這些基本資料型別都實現了比較方法
public class test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()){
String[] stringArray = new String[5];
for (int i = 0; i < 5; i++) {
String s = sc.nextLine();
stringArray[i] = s;
}
Arrays.sort(stringArray);
for (int i = 0; i < 5; i++) {
System.out.println(stringArray[i]);
}
}
}
}
hello
java
i
like
programming
hello
i
java
like
programming
使用sort方法可以實現對基本資料型別的字典排序
物件排序
讓實體類繼承Comparable介面,重寫CompareTo方法
public static void main(String[] args) {
ArrayList<student> list = new ArrayList<student>();
list.add(new student(20,"zhangsan",80,80));
list.add(new student(22,"lisi",75,90));
list.add(new student(30,"wangwu",90,65));
Collections.sort(list,new UserCompatator());
for (student stu:list){
System.out.println(stu.toString());
}
}
class student implements Comparable{
int age;
String name;
int chinese;
int math;
public student(int age, String name, int chinese, int math) {
this.age = age;
this.name = name;
this.chinese = chinese;
this.math = math;
}
@Override
public String toString() {
return "student{" +
"age=" + age +
", name='" + name + '\'' +
", chinese=" + chinese +
", math=" + math +
'}';
}
@Override
public int compareTo(Object o) {
return ((student) o).chinese - this.chinese;
}
}
該方法實現了基於語文成績的排序
使用lambda表示式
Collections.sort(stus,(e1,e2)->{
if(e1.chinese==e2.chinese){
return Integer.compare(e1.math,e2.math);
}else {
return Integer.compare(e1.chinese,e2.chinese);
}
});
for (student stu:stus){
System.out.println(stu);
}
構造介面Comparator介面的實現類,重寫Compare方法
public static void main(String[] args) {
ArrayList<student> list = new ArrayList<student>();
list.add(new student(20,"zhangsan",80,80));
list.add(new student(22,"lisi",75,90));
list.add(new student(30,"wangwu",90,65));
//原始順序
System.out.println("原始順序");
for (student stu:list){
System.out.println(stu.toString());
}
//按照年齡排序
System.out.println("按照年齡排序");
Collections.sort(list,new ageSort());
for (student stu:list){
System.out.println(stu.toString());
}
//按照姓名排序,字典序
System.out.println("按照姓名排序");
Collections.sort(list,new nameSort());
for (student stu:list){
System.out.println(stu.toString());
}
//按照語文成績排序
System.out.println("按照語文成績排序");
Collections.sort(list,new chineseSort());
for (student stu:list){
System.out.println(stu.toString());
}
}
}
class ageSort implements Comparator{ //簡單修改可實現逆序
@Override
public int compare(Object o1, Object o2) {
student s1 = (student)o1;
student s2 = (student)o2;
return s1.chinese < s2.chinese ? 1 : (s1 == s2 ? 0 : -1);
}
}
class nameSort implements Comparator{
@Override
public int compare(Object o1, Object o2) {
student s1 = (student)o1;
student s2 = (student)o2;
return s1.name.compareTo(s2.name);
}
}
class chineseSort implements Comparator{
@Override
public int compare(Object o1, Object o2) {
student s1 = (student)o1;
student s2 = (student)o2;
return s1.chinese - s2.chinese;
}
輸出:
原始順序
{age=20, name='zhangsan', chinese=80, math=80}
{age=22, name='lisi', chinese=75, math=90}
{age=30, name='wangwu', chinese=90, math=65}
按照年齡排序
{age=30, name='wangwu', chinese=90, math=65}
{age=20, name='zhangsan', chinese=80, math=80}
{age=22, name='lisi', chinese=75, math=90}
按照姓名排序
{age=22, name='lisi', chinese=75, math=90}
{age=30, name='wangwu', chinese=90, math=65}
{age=20, name='zhangsan', chinese=80, math=80}
按照語文成績排序
{age=22, name='lisi', chinese=75, math=90}
{age=20, name='zhangsan', chinese=80, math=80}
{age=30, name='wangwu', chinese=90, math=65}
二維陣列排序
Collections.sort(list, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
return o1[0]-o2[0];
}
});
Arrays.sort(str, new Comparator<String>(){
@Override
public int compare(String o1, String o2) {
String s1 = o1 + o2;
String s2 = o2 + o1;
return s1.compareTo(s2);
}
});
相關文章
- Java基礎-物件導向基礎Java物件
- [Java基礎]Class物件Java物件
- JAVA基礎--氣泡排序Java排序
- Java基礎系列—List排序Java排序
- [Java基礎]物件的生命週期Java物件
- Java基礎 --- 物件導向Java物件
- Java基礎11 物件引用Java物件
- JAVA物件導向基礎Java物件
- Java基礎-面相物件篇Java物件
- Java基礎| 類和物件Java物件
- JavaScript 基礎之物件ObjectJavaScript物件Object
- python 基礎之物件管理Python物件
- java基礎二:物件導向Java物件
- 夯實基礎:排序演算法之堆排序排序演算法
- Python基礎之物件導向Python物件
- Golang 基礎之物件導向Golang物件
- [java基礎]之常量Java
- java基礎之XMLJavaXML
- JAVA物件導向基礎--異常Java物件
- 理解Java中物件基礎Object類Java物件Object
- 【Java基礎】物件導向開發Java物件
- (Java筆記)物件導向基礎Java筆記物件
- JAVA物件導向基礎--物件導向介紹Java物件
- Python基礎之:Python中的內部物件Python物件
- Java 物件排序詳解Java物件排序
- ?Java基礎之Java列舉Java
- PHP基礎之物件導向篇PHP物件
- 夯實Java基礎系列1:Java物件導向三大特性(基礎篇)Java物件
- JAVA_基礎IO流物件流(三)Java物件
- Java基礎系列(七):物件與類(下)Java物件
- Java基礎之代理模式Java模式
- [java基礎]之註釋Java
- [java基礎]之變數Java變數
- [java基礎]之 運算子Java
- [java基礎]之JAVA關鍵字Java
- PHP基礎演算法之快速排序法PHP演算法排序
- 基礎演算法之排序演算法演算法排序
- java集合物件排序總結Java物件排序
- PHP基礎之物件導向講解PHP物件