使用按长度排序的比较器对名称的ArrayList进行排序

时间:2018-12-01 12:36:41

标签: java sorting arraylist comparator

我的代码有问题。我尝试将Strings用于arraylist,但是它不起作用。当我把类名放到arraylist中时,它只显示一个不同的值。我做了一个类来获取长度,因为在比较器中这样做是行不通的。它只是给我一个找不到符号的错误。

import java.util.*;
import java.io.*;

class StringLengthComparator implements Comparator<Name>
{
    public int compare(Name n1, Name n2)
    {
        return n1.getLength()-n2.getLength();
    }
}

public class Name implements Comparable<Name>
{
    public static String name;

    public Name(String n)
    {
        this.name = n;
    }

    public int compareTo(Name that)
    {
        return this.name.compareTo(that.name);
    }

    public String getName()
    {
        return this.name;
    }

    public int getLength()
    {
        return this.name.length();
    }

    public static void main(String[] args) throws Exception 
    {   
        ArrayList<Name> N = new ArrayList<>(5);     
        BufferedReader keyIn = new BufferedReader(new InputStreamReader(System.in));

        System.out.println("Please Enter Name: ");
        //String n = keyIn.readLine();


        for(int i=0;i<5;i++)
        {       
            String n = keyIn.readLine();
            N.add(new Name(n));
        }

        System.out.print("\n");
        Collections.sort(N);

        for(int i=0;i<N.size();i++)
        {
            System.out.println(N.get(i));
        }

        System.out.print("\n");

        Collections.sort(N, new StringLengthComparator());

        for(int i=0;i<N.size();i++)
        {
            System.out.println(N.get(i));
        }

    }
}

Error #1 for ArrayList <\ String> N =新的ArrayList <\ String>

Error #2 for ArrayList <\ Name> N =新的ArrayList <>(5)

2 个答案:

答案 0 :(得分:0)

如果要按长度排序,请按字母顺序排序,则需要2个编译器,并每次使用N.sort(new X)来选择要使用的编译器,其中X是所需编译器类的名称。
您不需要Collections.sort()

class StringLengthComparator implements Comparator<Name> {
    public int compare(Name n1, Name n2) {
        return n1.getLength()-n2.getLength();
    }
}

class StringCommonComparator implements Comparator<Name> {
    public int compare(Name n1, Name n2) {
        return n1.getName().compareTo(n2.getName());
    }
}

public class Name {
    private String name;

    public Name(String n) {
        this.name = n;
    }

    public String getName() {
        return this.name;
    }

    public int getLength() {
        return this.name.length();
    }

    public static void main(String[] args) throws Exception {
        ArrayList<Name> N = new ArrayList<>(5);
        BufferedReader keyIn = new BufferedReader(new InputStreamReader(System.in));

        System.out.println("Please Enter Name: ");
        String n = keyIn.readLine();
        N.add(new Name(n));

        for(int i=0; i<5; i++) {
            String name = keyIn.readLine();
            N.add(new Name(name));
        }

        System.out.println();
        System.out.println("Compare by Length");
        N.sort(new StringLengthComparator());

        for(int i=0;i<N.size();i++) {
            System.out.println(N.get(i).name);
        }

        System.out.println();
        System.out.println("Compare by String");
        N.sort(new StringCommonComparator());

        for(int i=0;i<N.size();i++) {
            System.out.println(N.get(i).name);
        }
    }
}

答案 1 :(得分:0)

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

class StringLengthComparator implements Comparator<Name> {
    @Override
    public int compare(Name n1, Name n2) {
        return n1.getLength() - n2.getLength();
    }
}

public class Name implements Comparable<Name> {
    public static String name;

    public Name(String n) {
        this.name = n;
    }

    @Override
    public int compareTo(Name that) {
        return this.name.compareTo(that.name);
    }

    public String getName() {
        return this.name;
    }

    public int getLength() {
        return this.name.length();
    }

    public static void main(String[] args) throws Exception {
        ArrayList<Name> N = new ArrayList<>(5);
        BufferedReader keyIn = new BufferedReader(new InputStreamReader(System.in));

        System.out.println("Please Enter Name: ");

        for (int i = 0; i < 5; i++) {
            String n = keyIn.readLine();
            N.add(new Name(n));
        }

        System.out.print("\n");
        Collections.sort(N);

        for (Name name : N) {
            System.out.println(name.getName());
        }

        System.out.print("\n");

        Collections.sort(N, new StringLengthComparator());

        for (int i = 0; i < N.size(); i++) {
            System.out.println(N.get(i));
        }

    }
}