collection java 反序_【Java小收获】使用Collection对ArrayList排序

正文之前

毕业论文肝到14000实在是肝不动了,必须开点新模块才能走得动,不然没法搞。所以最近在琢磨离散化这个东西。主要是参考的这个文献

773185d2a67d?utm_source=oschina-app

773185d2a67d?utm_source=oschina-app

我在Youtube上偶然看的一个小姐姐,然后痴迷于她的舞蹈。。

正文

好吧,这个只是个Part,做个笔记而已,不必较真!

这个是我当时参考的,结果不能排序!!

先把他的代码八一八:

package com.lj.test;

public class Student implements Comparable{

private String name;

private int age;

public Student() {

super();

}

public Student(String studnet, int age) {

super();

this.name = studnet;

this.age = age;

}

public String getStudnet() {

return name;

}

public void setStudnet(String studnet) {

this.name = studnet;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

@Override

public String toString() {

return "Student [studnet=" + name + ", age=" + age + "]";

}

@Override

public int compareTo(Student o) {

int num = this.age - o.age;

int num2 = num == 0 ?this.name.compareTo(o.name):num;

return num2;

}

}

这是应用!!

package com.lj.test;

import java.util.ArrayList;

import java.util.Collections;

public class ArrayListDemo3 {

public static void main(String[] args) {

ArrayList arrayList = new ArrayList();

Student s1 = new Student("pirlo",21);

Student s3 = new Student("范厨师",23);

Student s4 = new Student("马师傅",21);

Student s5 = new Student("非洲德化",24);

Student s6 = new Student("pirlo",22);

arrayList.add(s1);

arrayList.add(s3);

arrayList.add(s4);

arrayList.add(s5);

arrayList.add(s6);

Collections.sort(arrayList);

for(Student s : arrayList){

System.out.println(s);

}

}

}

结果,我跑起来根本没有那个效果,排序?不存在的!!

然后我还纳闷呢!!怎么回事呢??

Comparator返回值在jdk1.7、jdk1.8里必须是一对相反数,如1和-1,不能是1和0.因为1.7的排序算法采用timsort,对返回值有严格要求这是重头戏!!然后我就可以了!!

import java.util.Scanner;

import java.util.ArrayList;

import java.util.Collections;

class NewData implements Comparable{

private float sensor;

private float category;

NewData(float a, float b){

super();

this.sensor = a;

this.category = b;

}

@Override

public String toString() {

return "\n[Sensor:" + sensor + ", category=" + category + "]";

}

@Override

public int compareTo(NewData o) {

return this.sensor>o.sensor?1:-1;

}

}

public class EADC{

public static void main(String[] args) {

float[][] dat = new float[15][6];

Scanner in = new Scanner(System.in);

for(int i=0; i<15;++i){

for(int s=0;s<6;++s){

dat[i][s] = in.nextFloat();

}

}

ArrayList sensor = new ArrayList<>();

ArrayList LIST = new ArrayList<>();

for(int i=0;i<15;++i){

if(!sensor.contains(dat[i][1])){

sensor.add(dat[i][1]);

LIST.add(new NewData(dat[i][1], dat[i][5]));

//便利旧集合没有就添加到新集合

}

}

Collections.sort(LIST);

System.out.println(LIST);

}

}

结果,很美:

/Library/Java/JavaVirtualMachines/jdk-9.0.1.jdk/Contents/Home/bin/java "-javaagent:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=50679:/Applications/IntelliJ IDEA.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath /Users/zhangzhaobo/IdeaProjects/Test_of_Java/out/production/Test_of_Java EADC

2.0 2.0 -2.0 -2.0 0.0 0.0

-1.0 -4.0 3.0 0.0 0.0 0.0

-0.0 -2.0 0.0 -6.0 0.0 0.0

-0.0 0.0 -1.0 -1.0 0.0 0.0

5.0 2.0 -4.0 2.0 0.0 0.0

-1.0 5.0 -1.0 -3.0 0.0 0.0

-4.0 1.0 -0.0 -2.0 0.0 0.0

-6.0 8.0 2.0 -1.0 0.0 0.0

1.0 -0.0 -0.0 -0.0 0.0 0.0

-3.0 -2.0 0.0 2.0 0.0 0.0

2.0 -1.0 1.0 0.0 0.0 0.0

4.0 -1.0 -2.0 -0.0 0.0 0.0

-4.0 2.0 -1.0 -0.0 0.0 0.0

0.0 10.0 -1.0 -4.0 0.0 0.0

2.0 7.0 2.0 -0.0 0.0 0.0 [

[Sensor:-4.0, category=0.0],

[Sensor:-2.0, category=0.0],

[Sensor:-1.0, category=0.0],

[Sensor:-0.0, category=0.0],

[Sensor:0.0, category=0.0],

[Sensor:1.0, category=0.0],

[Sensor:2.0, category=0.0],

[Sensor:5.0, category=0.0],

[Sensor:7.0, category=0.0],

[Sensor:8.0, category=0.0],

[Sensor:10.0, category=0.0]]

Process finished with exit code 0

完美运行,所以说自定义的sort要返回值是-1 和 1!不能是1,0

正文之后

溜了,继续肝!!!明天还有音控员的笔试。。日了狗了!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值