JAVA-帮助在我的类中添加可比较的接口和数组

JAVA-帮助在我的类中添加可比较的接口和数组,java,arrays,string,comparable,compareto,Java,Arrays,String,Comparable,Compareto,我得到了一份关于数组的实验任务,对数组进行排序,并为我的两个类添加了可比较的接口。我必须修改Customer类,使其实现一个可比较的接口。然后我必须对这个类创建的对象数组进行排序 以下是我的工作表概述的步骤: 打开customer和SortedCustomersApp java文件(见下文): 公共类客户 { private String email; private String firstName; private String lastName; public Customer(Str

我得到了一份关于数组的实验任务,对数组进行排序,并为我的两个类添加了可比较的接口。我必须修改Customer类,使其实现一个可比较的接口。然后我必须对这个类创建的对象数组进行排序

以下是我的工作表概述的步骤:

  • 打开customer和SortedCustomersApp java文件(见下文):

    公共类客户

    {
    
    private String email;
    private String firstName;
    private String lastName;
    
    public Customer(String email, String firstName, String lastName)
    {
        this.email = email;
        this.firstName = firstName;
        this.lastName = lastName;
    }
    
    public void setEmail(String email)
    {
        this.email = email;
    }
    
    public String getEmail()
    {
        return email;
    }
    
    public void setFirstName(String firstName)
    {
        this.firstName = firstName;
    }
    
    public String getFirstName()
    {
        return firstName;
    }
    
    public void setLastName(String lastName)
    {
        this.lastName = lastName;
    }
    
    public String getLastName()
    {
        return lastName;
    }
    }
    
  • 这是分类客户应用程序:

    import java.util.Arrays;
    
    public class SortedCustomersApp
    {
        public static void main(String[] args)
        {
    
        }
    }
    
  • 向customer类添加代码以实现可比较的接口。您创建的compareTo方法应将当前客户的电子邮件字段与其他客户的电子邮件字段进行比较。为此,您不能使用<或>运算符,因为电子邮件字段是字符串。相反,请使用string类的compareToIgnoreCase方法。此方法将执行它的字符串与作为参数传递给它的字符串进行比较。如果第一个字符串小于第二个字符串,则此方法返回一个负整数。如果第一个字符串大于第二个字符串,则返回一个正整数。如果两个字符串相等,则返回0
  • 3.将代码添加到SortedCustomersApp类,该类创建一个可容纳3个元素的客户对象数组,并创建客户对象并将其分配给这些元素。确保指定给对象的电子邮件值不是按字母顺序排列的。对数组进行排序

  • 编码一个“for each”循环,将每个客户对象的email、firstName和lastName字段打印在单独的一行上

  • 编译并测试程序


  • 此程序需要为电子邮件、firstName和lastName提供用户输入,但当我尝试将用户输入添加到客户应用程序时,我收到错误消息,说我无法将字符串转换为扫描类型,但用户输入必须是字符串,所以这也是一个问题。

    为了定义
    集合,您实现了Comaprable。sort()
    您使用什么标准比较实例。例如,如果我要按学生的成绩比较学生:

    public class Student implements Comparable<Student>{
    
        int age;
        int grade;
    
    
        //this method is used in Collections.sort() to determine what is bigger
        //for me its grades that matter so that's what I do in  code
        @override
        compareTo(Student other){
    
         if(this.grade>other.grade){
    
            return 1;   
         }else if(grade<other.grade){
    
           return -1;
          }
    
           return 0;
        }
    
    
    
    }
    

    编辑:在评论中回答您的问题。如果使用
    集合
    数组列表
    则调用
    集合.sort()
    ,如果使用常规数组,则调用
    数组.sort()
    。在这两种情况下,要排序的对象都需要实现
    compariable
    ,就像我向您展示的那样。

    您可以比较两个字符串的字典式调用comparieto方法对字符串对象,代码如下所示

    public class Customer implements Comparable<Customer>{
    
    private String email;
    
    public Customer(String email) {
        this.email = email;
    
    }
    
    public void setEmail(String email) {
        this.email = email;
    }
    
    public String getEmail() {
        return email;
    }
    
    
    @Override
    public int compareTo(Customer otherCustomer) {
        return this.email.compareToIgnoreCase(otherCustomer.email);
    }
    
    @Override
    public String toString() {
        return "Customer{" +
                "email='" + email + '\'' +
                '}';
    }
    
    公共类客户实现可比较{
    私人字符串电子邮件;
    公共客户(字符串电子邮件){
    this.email=电子邮件;
    }
    公用电子邮件(字符串电子邮件){
    this.email=电子邮件;
    }
    公共字符串getEmail(){
    回复邮件;
    }
    @凌驾
    公共int比较(客户-其他客户){
    返回此.email.compareTignoreCase(otherCustomer.email);
    }
    @凌驾
    公共字符串toString(){
    返回“客户{”+
    “电子邮件='”+电子邮件+'\''+
    '}';
    }
    
    }

    可以使用Collections.sort方法对对象列表进行排序

     List<Customer> list = new ArrayList<>();
    
        list.add(new Customer("abc@bc.com"));
        list.add(new Customer("des@bc.com"));
        list.add(new Customer("bbc@bc.com"));
        list.add(new Customer("aec@bc.com"));
        Collections.sort(list);
    
    List List=new ArrayList();
    列表。添加(新客户(“abc@bc.com"));
    列表。添加(新客户(“des@bc.com"));
    列表。添加(新客户(“bbc@bc.com"));
    列表。添加(新客户(“aec@bc.com"));
    集合。排序(列表);
    
    修改的客户类别

    public class Customer implements Comparable<Customer>{
        :
        public int compareTo(Object obj)
        {
            Customer cus = (Customer) obj;
            // write your comparison code here
            // return 1, 0, or -1 on the basis of your requirement
    
        }    
        :
    }
    
    在主方法中对数据进行排序

    有两种方法可以将字符串作为用户的输入:
    1) 缓冲读取器

     BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
     String email = reader.readLine(); // email, firstname etc.
    

    2)扫描仪类别

    Scanner input = new Scanner(System.in);
    String email = input.next();
    

    现在创建一个Customer对象并添加setter方法来设置值。

    首先,感谢您的输入!非常感谢!其次,我的书没有提到任何关于collections.sort的内容,只提到了array类的方法,包括Arrays.sort。它们可以互换吗?另外,你的例子写得很好,但是我在创建一个使用字符串而不是数值的compareTo方法时遇到了更多的困难。另外,你打算如何比较两个客户?从词典学的角度看?我的教授对她想要什么很含糊,我想从词典学的角度看,因为她所说的是,如果两个电子邮件字符串相同,最终结果可能是负数、正数或0。我正在考虑这样做:(this.email.compareToIgnoreCase(other.email)>0)或(this.email.compareToIgnoreCase(other.email)我的另一个问题是,我应该只向上面已经给我的java文件添加代码,而不更改已经存在的代码(特别是customer类)。我正在尝试添加用户输入,但每次我在主sortedCustomersApp中编写代码,创建客户对象并引用其设置方法时,请在引用方法的括号中放置一个扫描对象以添加用户输入,但我总是会收到错误“无法将字符串转换为扫描程序”。
     BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
     String email = reader.readLine(); // email, firstname etc.
    
    Scanner input = new Scanner(System.in);
    String email = input.next();