JAVA如何以数组中的一列作为标准进行排序。

这是对对象数组进行排序,有两种方法:1、一种是实现Comparable接口,复写compareTo()方法。2、另一种是自定义一个比较器即实现Comparator接口,复写compare()方法。import java.util.Arrays;import java.util.Comparator;class Student { public int id;public String name;public int age;public ...
JAVA如何以数组中的一列作为标准进行排序。
你这是对对象实现比较大小。这个对对象的编译器肯定不知道。要你自己做好告诉编译器我这个对象是个大的对象。在java中有两种方式
1:实现Comparable 接口 的 public int compareTo(T o) 方法;

2:实现Comparator 接口 的 int compare(T o1, T o2)方法;
在这里你没有贴出你Student类的源码。我就假定只有三个成员变量。如下:

class Student implements Comparable{
private int no, grade;
private String name;
public Student(int no, String name, int grade){
this. no = no;
this.grade = grade;
this.name = name;
}

public int getGrade(){
return grade;
}
//override
public int compareTo(Object arg0){
Student stu = (Student) arg0;
if(this.getGrade()>=arg0.getGrade())
return this.getGrade;
else
return 0;

}
}

然后在你的主函数里面
private Student stu[]= {new Student(1,"张三",2),new Student(2,"李四",4),new Student(3,"王五",1),new Student(4,"赵六",0)};

for(int i = 0 ; i< stu.length-1; i++){
if(stu[i].compareto(stu[i+1]))=0)//如果比较等于0, 说明i<i+1,然后一环
Student student = stu[i];
stu[i] = stu[i+1];
stu[i+1] = student;
}

//// 补充/////
有了比较之后,那就是排序的事情了。 上面写错了。。。。 最起码要用个冒泡排序。 这只是两两一环。。。 。。 不过交换的方法就是上面写的。排序的算法就不写了。。 自己看书就可以啦2014-11-25
这是对对象数组进行排序,有两种方法:1、一种是实现Comparable接口,复写compareTo()方法。2、另一种是自定义一个比较器即实现Comparator接口,复写compare()方法。

import java.util.Arrays;
import java.util.Comparator;
class Student
{
public int id;
public String name;
public int age;
public Student(int id,String name,int age){
this.id=id;
this.name=name;
this.age=age;
}
public String toString(){
return "根据第三列排序:"+this.name+".."+this.age;
}
}
class StudentComparator implements Comparator<Student>//定义一个Student比较器
{
public int compare(Student stu1,Student stu2){
if(stu1==stu2){
return 0;
}
if(stu1.age<stu2.age){
return 1;
}else if(stu1.age>stu2.age){
return -1;
}else{
return 0;
}
}
}
public class StudentTest
{
public static void main(String[] args){
Student stu[]= {new Student(1,"张三",2),new Student(2,"李四",4),
new Student(3,"王五",1),new Student(4,"赵六",0)};
StudentComparator sc=new StudentComparator();
Arrays.sort(stu,sc);
for(int i=0;i<stu.length;i++){
System.out.println(stu[i]);
}
}
}
2014-11-25
mengvlog 阅读 10 次 更新于 2025-07-20 00:15:59 我来答关注问题0
  • 这是对对象数组进行排序,有两种方法:1、一种是实现Comparable接口,复写compareTo()方法。2、另一种是自定义一个比较器即实现Comparator接口,复写compare()方法。import java.util.Arrays;import java.util.Comparator;class Student { public int id;public String name;public int age;public ...

  • 以ArrayList为例,可以通过Arrays.asList方法将数组转换为List。这种方法返回一个固定大小的列表,任何对返回列表的修改都会直接反映到原始数组中。下面是一个实例代码:public static void main(String[] args) { List list = Arrays.asList(new String[]{"ss","sss"});String[] a = list.toArray...

  • java List list = new ArrayList>();// 向list中添加元素 ArrayList convertedArrayList = (ArrayList) list;请注意,将List转换为ArrayList时,实际创建的是一个新的ArrayList对象,原始的list对象不会受到影响。此转换仅在需要使用ArrayList类的特定方法时有用,如在遍历、排序或基于索引的操作。LinkedList...

  •  翡希信息咨询 JAVA中怎么让一个数加入到数组中

    使用数组的索引来将值赋给数组的某个位置。例如,如果数组int[] i = new int[5];已经创建,你可以通过i[索引] = 值;的方式来加入新元素。示例:假设你已经有一个长度为5的整数数组int[] i = new int[5];,并且你想将数字6加入到数组的最后一个位置,可以这样做:javaint[] i = new int...

  •  海南加宸 java怎样实现把从数据库中某一字段数据存入一维数组中

    首先,我们需要创建一个Java类,比如命名为“TestColumnToArray”,并在其中定义一个主方法。主方法中先实例化一个TestColumnToArray对象,然后调用其内部的方法来连接数据库,并查找指定字段的数据。在“TestColumnToArray”类中,我们定义了一个名为“connDB”的私有方法,用于连接数据库。接着定义了一...

檬味博客在线解答立即免费咨询

Java相关话题

Copyright © 2023 WWW.MENGVLOG.COM - 檬味博客
返回顶部