Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.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 Collections.sort()方法不起作用_Java - Fatal编程技术网

Java Collections.sort()方法不起作用

Java Collections.sort()方法不起作用,java,Java,在根据名称对名称和年龄作为属性的Customer类(用户定义)的arraylist进行排序时,Collections.sort()方法显示错误,即“类型java.util.Comparator未解析。它是从required.Class文件间接引用的 package comparable; import java.util.*; public class Tester { public static void main(String[] args){ List<Cu

在根据名称对名称和年龄作为属性的Customer类(用户定义)的arraylist进行排序时,Collections.sort()方法显示错误,即“类型java.util.Comparator未解析。它是从required.Class文件间接引用的

package comparable;
import java.util.*;

public class Tester {
    public static void main(String[] args){
        List<Customer> custtlist=new ArrayList<Customer>();
        Customer c1=new Customer("vikas",1);
        Customer c2=new Customer("mittal",2);
        custtlist.add(c1);
        custtlist.add(c2);

        System.out.println("Before Sorting");
        Iterator<Customer> iterator = custtlist.iterator();
        while(iterator.hasNext()){
            Customer customer = (Customer) iterator.next();
            System.out.println(customer.getCustname());
        }
        Collections.sort(custtlist);   


        System.out.println("After Sorting");
        iterator = custList.iterator();

        while (iterator.hasNext()) {
            Customer customer = (Customer) iterator.next();
            System.out.println(customer.getCustName());
        }
    }
}

//Customer Class
package comparable;

public class Customer implements Comparable<Customer> {
    private String custname;
    private int age;

    public Customer(String custname, int age) {
        this.custname = custname;
        this.age = age;
    }

    public Customer() {
    }

    public String getCustname() {
        return custname;
    }

    public void setCustname(String custname) {
        this.custname = custname;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int compareTo(Customer c){
        return this.custname.compareTo(c.getCustname());
    }
}
可比包装;
导入java.util.*;
公共类测试员{
公共静态void main(字符串[]args){
List custtlist=new ArrayList();
客户c1=新客户(“vikas”,1);
客户c2=新客户(“米塔尔”,2);
客户名单增补(c1);
新增客户名单(c2);
System.out.println(“排序前”);
迭代器迭代器=custtlist.Iterator();
while(iterator.hasNext()){
Customer=(Customer)迭代器.next();
System.out.println(customer.getCustname());
}
集合。排序(客户列表);
System.out.println(“排序后”);
迭代器=custList.iterator();
while(iterator.hasNext()){
Customer=(Customer)迭代器.next();
System.out.println(customer.getCustName());
}
}
}
//客户类别
包装可比;
公共类客户实现可比较{
私有字符串名称;
私人互联网;
公共客户(字符串custname,int age){
this.custname=custname;
这个。年龄=年龄;
}
公众客户(){
}
公共字符串getCustname(){
返回客户名称;
}
public void setCustname(字符串custname){
this.custname=custname;
}
公共整数getAge(){
回归年龄;
}
公共无效设置(整数){
这个。年龄=年龄;
}
公共内部比较(客户c){
返回这个.custname.compareTo(c.getCustname());
}
}

您的代码中有很多拼写错误。在我更正它们之后,编译成功了。我冒昧地做了一些改进和样式设置

import java.util.*;

public class Tester {
    public static void main(String[] args){
        List<Customer> customersList = new ArrayList<Customer>();
        Customer c1 = new Customer("vikas", 1);
        Customer c2 = new Customer("mittal", 2);
        customersList.add(c1);
        customersList.add(c2);

        System.out.println("Before Sorting");
        for(Customer customer : customersList) {
            System.out.println(customer.getName());
        }

        Collections.sort(customersList);
        System.out.println("\nAfter Sorting");

        for(Customer customer : customersList) {
            System.out.println(customer.getName());
        }
    }
}

public class Customer implements Comparable<Customer> {
    private String name;
    private int age;

    public Customer(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public Customer() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int compareTo(Customer c){
        return this.name.compareTo(c.getName());
    }
}
import java.util.*;
公共类测试员{
公共静态void main(字符串[]args){
List CustomerList=新建ArrayList();
客户c1=新客户(“vikas”,1);
客户c2=新客户(“米塔尔”,2);
customersList.add(c1);
customersList.add(c2);
System.out.println(“排序前”);
用于(客户:客户列表){
System.out.println(customer.getName());
}
Collections.sort(customersList);
System.out.println(“\n排序后”);
用于(客户:客户列表){
System.out.println(customer.getName());
}
}
}
公共类客户实现可比较{
私有字符串名称;
私人互联网;
公共客户(字符串名称,整数){
this.name=名称;
这个。年龄=年龄;
}
公众客户(){
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公共整数getAge(){
回归年龄;
}
公共无效设置(整数){
这个。年龄=年龄;
}
公共内部比较(客户c){
返回这个.name.compareTo(c.getName());
}
}

也许你的Customer.compareTo方法没有很好地实现。你能添加你的相关代码吗?我已经测试了你的代码,它可以工作。你有两个输入错误“iterator=custList.iterator();“应该是”迭代器=custtlist.iterator();和“System.out.println(customer.getCustName());“应该是”System.out.println(customer.getCustname());“。排序方法按预期工作。