正文之前
毕业论文肝到14000实在是肝不动了,必须开点新模块才能走得动,不然没法搞。所以最近在琢磨离散化这个东西。主要是参考的这个文献
我在Youtube上偶然看的一个小姐姐,然后痴迷于她的舞蹈。。
正文
好吧,这个只是个Part,做个笔记而已,不必较真!
这个是我当时参考的,结果不能排序!!
先把他的代码八一八:
package com.lj.test;
public class Student implements Comparable<Student>{
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<Student> arrayList = new ArrayList<Student>();
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);
}
}
}
结果,我跑起来根本没有那个效果,排序?不存在的!!
然后我还纳闷呢!!怎么回事呢??
直到后来,又看了一个博客!
如何实现对ArrayList排序 sort() - CSDN博客
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<NewData>{
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<Float> sensor = new ArrayList<>();
ArrayList<NewData> 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
正文之后
溜了,继续肝!!!明天还有音控员的笔试。。日了狗了!