首页 > 编程知识 正文

在一个类中创建另一个类的对象,jna 结构体 字符串 引用

时间:2023-05-05 09:37:07 阅读:21855 作者:4320

Java类代替c结构体实现对应的排序输出

文章目录c结构体排序在Java中采用类实现相同功能的问题

在c结构排序下显示了一些内联代码片。

# include iostream # includealgorithmusingnamespacestd; 结构节点{ string name; //名称int id; //id号int cj; //成绩}; boolCMP(nodex,node y ) {return x.cjy.cj; (}int main ) ) {int t; cint; 节点a [ t ]; for(intI=0; it; I ) {cina[i].namea[i].ida[i].cj; }sort(a,a t,cmp ); for(intI=0; it; I ) couta [ I ].name ' ' a [ I ].id ' ' a [ I ].CJ endl; }返回0; }这里只是对成绩进行排名,成绩相同的情况下没有写。 根据需要添加到cmp函数中即可。

如何在Java中采用类实现相同的功能1

使用Student类实现Comparable接口的第一种方法是使用结构而不是Student类实现Student类的compareTo方法。 import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner; //如何按类而不是结构进行排序publicclassldtjgtpx (publicstaticvoidmain (字符串[ ] args ) scannersc=newscanner ) system.in ); while(sc.hasnext () ) /一直循环({ int n=sc.nextInt ) ); liststudentstudents=new ArrayList (; for(intI=0; in; I({stringname=sc.next ); int age=sc.nextInt (; int cj=sc.nextInt (; sudents.add(newstudent ) name,age,cj ); }collections.sort(students; for (student student 3360 students (system.out.println ) student.getAge ) ' student.getAge ) ' student.geent ) ) } } classstudentimplementscomparablestudent { string name; int age; int cj; //成绩publicstudent(stringname,int age,int cj ) ) { this.name=name; this.age=age; this.cj=cj; } public String getName (() { return name; }公共语音集名称(字符串名称) { this.name=name; } public int getAge () { return age; }公共语音设置(intage ) { this.age=age; } public int getCj () { return cj; }publicvoidsetcj(intCJ ) { this.cj=cj; } @ overridepublicintcompareto (studento ) if ) this.getcj )==o.getCj ) /分数相同时) if ) this.getname ).eqtname //回到年龄小的

} return this.getName().compareTo(o.getName());//名字不一样时,返回名字小的 } return new Integer(this.getCj()).compareTo(o.getCj()); }}

方法二

第二种还是和第一种方法一样,通过实现Comparable接口的方法但是比实现compareTo方法时更加简单,更加容易理解。 import java.util.*;//另一种代替结构体排序方法public class ldtjgtpx1 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); while(sc.hasNext())//一直循环 { int n=sc.nextInt(); List<Student1> students=new ArrayList<>(); for(int i=0;i<n;i++) { String name=sc.next(); int age=sc.nextInt(); int cj=sc.nextInt(); students.add(new Student1(name,age,cj)); } Collections.sort(students); for(Student1 student:students) { System.out.println(student.name+" "+student.age+" "+student.cj); } } }}class Student1 implements Comparable<Student1>{ String name; int age; int cj; public Student1(String name, int age, int cj) { this.name = name; this.age = age; this.cj = cj; } @Override public int compareTo(Student1 o) { if(this.cj==o.cj) { return this.age-o.age; } return this.cj-o.cj;//分数从小到大排序 }}

方法三

第三种写法,利用Comparator接口,注意它和Comparable的区别,后者是内部的,前者是外部的。 import java.util.*;public class ldtjgtpx2 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); List<Student2> students2=new ArrayList<>(); while(n-->0) { String name=sc.next(); int id=sc.nextInt(); int cj=sc.nextInt(); Student2 student2=new Student2(name,id,cj); students2.add(student2); } paixu paixu=new paixu(); Collections.sort(students2,paixu); for(Student2 student2:students2) { System.out.println(student2.name+" "+student2.id+" "+student2.cj); } } }}class Student2{ String name; int id; int cj; public Student2(String name, int id, int cj) { this.name = name; this.id = id; this.cj = cj; }}//外部class paixu implements Comparator<Student2>{ @Override public int compare(Student2 o1, Student2 o2) { return o1.cj-o2.cj; }}

方法四

第4种方法,也是我认为最简单的方法,直接在sort时把排序逻辑实现了,也推荐大家用这种方法,力扣或者牛客网的ysdgb都是直接这样写。 import java.util.*;//第4种方法,最方便的方法public class ldtjgtpx3 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); List<Student3> students3=new ArrayList<>(); while(n-->0) { String name=sc.next(); int id=sc.nextInt(); int cj=sc.nextInt(); Student3 student3=new Student3(name,id,cj); students3.add(student3); } Collections.sort(students3, new Comparator<Student3>() { @Override public int compare(Student3 o1, Student3 o2) { return o1.cj-o2.cj; } }); for(Student3 student3:students3) { System.out.println(student3.name+" "+student3.id+" "+student3.cj); } } }}class Student3{ String name; int id; int cj; public Student3(String name, int id, int cj) { this.name = name; this.id = id; this.cj = cj; }} 提问 以上java的方法进行排序都是将整个列表一起排序从小到大排序的,假如我只是想排序某个区间(某个具体的部分),有什么办法呢?C++里面的sort函数,我可以修改成sort(a+fromindex,a+toindex,cmp)方法去实现具体某个部分,那么java中该如何简单的实现呢?

欢迎大家在评论区留下你们的发言和见解,谢谢。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。