Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
确保java中对象的顺序_Java - Fatal编程技术网

确保java中对象的顺序

确保java中对象的顺序,java,Java,我有一节课看起来像这样 class Student { public String str; public int marks; } 说三个物体为 学生S1(str:sub1,分数:10),S2(str:sub2,分数:5),S3(str:S3,分数:2) 我想确保对于学生对象,如果sub1>sub2>sub3,则此条件成立,那么marks1>marks2>marks3将该类实现为: class Student implements Comprable<Stude

我有一节课看起来像这样

class Student {
   public String str;   
   public int    marks;
}
说三个物体为

学生S1(str:sub1,分数:10),S2(str:sub2,分数:5),S3(str:S3,分数:2)


我想确保对于学生对象,如果sub1>sub2>sub3,则此条件成立,那么marks1>marks2>marks3

将该类实现为:

class Student implements Comprable<Student> { 
....
    public int compareTo( Student other ) { 
        return this.marks - other.marks;
    }
 ...
}
class学生实现可压缩{
....
公共国际比较(学生其他){
返回此.marks-其他.marks;
}
...
}
这是一个工作演示。在这个演示中,类的自然顺序是“根据marks属性递减”。也就是说,较大的标记将置于较低的标记之前

import java.util.*;
class Student implements Comparable<Student> {
   public String str;   
   public int  marks;

   public Student( String s, int m ) { 
     this.str = s;
     this.marks = m;
   }
   public int compareTo( Student other ) { 
    return other.marks - this.marks;
   }
   public String toString() { 
     return String.format("Student(str=%s,marks=%s)", str, marks);
   }

}
class Main { 
  public static void main( String ... args ) { 
    List<Student> l;
    Collections.sort(( l =  new ArrayList<Student>(Arrays.asList(
      new Student("sub3", 2),
      new Student("sub1", 10),
      new Student("sub2", 5)
    ))));
    System.out.println( l );
  }
}


C:\>java Main
[Student(str=sub1,marks=10), Student(str=sub2,marks=5), Student(str=sub3,marks=2)]
import java.util.*;
班级学生实行可比性{
公共字符串str;
公共int标志;
公立学生(字符串s,int m){
this.str=s;
这个。马克=m;
}
公共国际比较(学生其他){
返回other.marks-this.marks;
}
公共字符串toString(){
返回String.format(“Student(str=%s,marks=%s)”,str,marks);
}
}
类主{
公共静态void main(字符串…参数){
清单l;
Collections.sort((l=newArrayList(Arrays.asList(
新生(“sub3”,2),
新生(“sub1”,10),
新生(“sub2”,5)
))));
系统输出打印LN(l);
}
}
C:\>Javamain
[学生(str=sub1,分数=10),学生(str=sub2,分数=5),学生(str=sub3,分数=2)]

让该类实现可比性:

class Student implements Comprable<Student> { 
....
    public int compareTo( Student other ) { 
        return this.marks - other.marks;
    }
 ...
}
class学生实现可压缩{
....
公共国际比较(学生其他){
返回此.marks-其他.marks;
}
...
}
这是一个工作演示。在这个演示中,类的自然顺序是“根据marks属性递减”。也就是说,较大的标记将置于较低的标记之前

import java.util.*;
class Student implements Comparable<Student> {
   public String str;   
   public int  marks;

   public Student( String s, int m ) { 
     this.str = s;
     this.marks = m;
   }
   public int compareTo( Student other ) { 
    return other.marks - this.marks;
   }
   public String toString() { 
     return String.format("Student(str=%s,marks=%s)", str, marks);
   }

}
class Main { 
  public static void main( String ... args ) { 
    List<Student> l;
    Collections.sort(( l =  new ArrayList<Student>(Arrays.asList(
      new Student("sub3", 2),
      new Student("sub1", 10),
      new Student("sub2", 5)
    ))));
    System.out.println( l );
  }
}


C:\>java Main
[Student(str=sub1,marks=10), Student(str=sub2,marks=5), Student(str=sub3,marks=2)]
import java.util.*;
班级学生实行可比性{
公共字符串str;
公共int标志;
公立学生(字符串s,int m){
this.str=s;
这个。马克=m;
}
公共国际比较(学生其他){
返回other.marks-this.marks;
}
公共字符串toString(){
返回String.format(“Student(str=%s,marks=%s)”,str,marks);
}
}
类主{
公共静态void main(字符串…参数){
清单l;
Collections.sort((l=newArrayList(Arrays.asList(
新生(“sub3”,2),
新生(“sub1”,10),
新生(“sub2”,5)
))));
系统输出打印LN(l);
}
}
C:\>Javamain
[学生(str=sub1,分数=10),学生(str=sub2,分数=5),学生(str=sub3,分数=2)]

我还需要确保sub1>sub2>sub3以及marks1>marks2>marks3都是这样做的。在这里阅读更多信息:虽然在本例中这可能不是问题,但在使用减法实现compareTo方法时要小心。发生下溢将是灾难性的。它应该只在最简单的应用程序中使用。(注意-不要认为这不是一个很好的答案,因为它肯定得到了我的支持。)我还需要确保sub1>sub2>sub3以及marks1>marks2>Marks3都是这样做的。在这里阅读更多信息:虽然在本例中这可能不是问题,但在使用减法实现compareTo方法时要小心。发生下溢将是灾难性的。它应该只在最简单的应用程序中使用。(注意——不要认为这不是一个很好的答案,因为这肯定得到了我的支持。)