按double和String排序arraylist

时间:2019-02-12 14:28:34

标签: java eclipse arraylist

我们被赋予以下任务:

  • 按照狗的尾巴长度(两倍)对数组列表进行排序
  • 如果两条或更多的狗的尾巴长度相同,则按名称排序

我能够创建一个代码,但是在第一次打印时未显示正确的结果。我必须再次尝试才能真正进行排序。

我对编程非常陌生,这使我感到困惑。有任何想法吗?谢谢!

private void listDogs() {
    boolean length = false;
    for (int i = 0; i < dogs.size(); i++) {
        length = true;
    }
    if (dogs.isEmpty()) {
        System.out.println("Error: no dogs in register");

    }else {
        System.out.println("Please enter the tail lenght minimum: ");
        double tailLength = scan.nextDouble();
        scan.nextLine();

        Collections.sort(dogs);
        for (int i = 0; i < dogs.size(); i++) {
            if (dogs.get(i).getTailLength() >= tailLength) {
                System.out.println(dogs.get(i));
                length = true;
            }
        }
        if (length == false) {
                System.out.println("Error: No dog is registered with this tail length.");   
        }       

@Override
public int compareTo(Dog o) {
    // TODO Auto-generated method stub
    int compare = Double.compare(tailLength, o.tailLength);   
    if (compare == 0) {
        compare = Double.compare(tailLength, o.tailLength);    
    }
    if (compare == 0) {
        compare = name.compareTo(o.name);
    }
    return compare;
}

2 个答案:

答案 0 :(得分:2)

我不确定这是否可以解决您的问题,但是您有

scan.nextLine();

在您的代码中,我不知道这是否必须存在,因为据我所知,它仅等待您输入内容,然后甚至不将其保存在变量中。因此,您可以删除此行(第12行)。

答案 1 :(得分:1)

您尝试这个吗?

import java.util.ArrayList;
import java.util.Collections;

public class Dog implements Comparable<Dog> {

    private double tailLength;
    private String name;

    public Dog(final double _tailLength, final String _name) {
        tailLength = _tailLength;
        name = _name;
    }

    @Override
    public String toString() {
        return "Dog [tailLength=" + tailLength + ", name=" + name + "]";
    }

    @Override
    public int compareTo(final Dog o) {
        int res = Double.compare(tailLength, o.getTailLength());
        if (res == 0) {
            res = name.compareTo(o.getName());
        }
        return res;
    }

    public double getTailLength() {
        return tailLength;
    }

    public void setTailLength(final double tailLength) {
        this.tailLength = tailLength;
    }

    public String getName() {
        return name;
    }

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

    public static void main(final String[] args) {
        final ArrayList<Dog> dogs = new ArrayList<Dog>();
        dogs.add(new Dog(2, "Dog D"));
        dogs.add(new Dog(2, "Dog A"));
        dogs.add(new Dog(5, "Dog C"));
        dogs.add(new Dog(4, "Dog A"));
        dogs.add(new Dog(3, "Dog A"));
        dogs.add(new Dog(3, "Dog B"));
        dogs.add(new Dog(1, "Dog A"));

        // Sort Dog by tailLength and name
        Collections.sort(dogs);

        final Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("tailLength: ");
            final double inputTaillength = sc.nextDouble();
            for (final Dog dog : dogs) {
                if (dog.tailLength == inputTaillength) {
                    System.out.println(dog);
                }
            }

        }
    }
}