有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用ArrayList对Java中最高和最低的对象进行排序?

编写一个方法返回列表中最常见的玩具,并编写另一个方法按计数对玩具进行排序

这是我的密码

import java.util.ArrayList;

public class ToyStore {
    private ArrayList<Toy> toyList;

    public ToyStore() {
    }

    public void loadToys(String toys) {
        toyList = new ArrayList<Toy>();
        for (String item : toys.split(" ")) {
            Toy t = getThatToy(item);
            if (t == null) {
                toyList.add(new Toy(item));
            } else {
                t.setCount(t.getCount() + 1);
            }
        }
    }

    public Toy getThatToy(String nm) {
        for (Toy item : toyList) {
            if (item.getName().equals(nm)) {
                return item;
            }
        }
        return null;
    }

    public String getMostFrequentToy() {
        int position = 0;
        int maximum = Integer.MIN_VALUE;
        for (int i = toyList.size() - 1; i >= 0; i--) {
            if (toyList.get(i).getCount() > maximum)
                maximum = toyList.get(i).getCount();
            position = i;
        }
        return toyList.get(position).getName();
    }

    public void sortToysByCount() {
        ArrayList<Toy> t = new ArrayList<Toy>();
        int count = 0;
        int size = toyList.size();

        for (int i = size; i > 0; i--) {
            t.add(new Toy(getMostFrequentToy()));
            t.get(count).setCount(getThatToy(getMostFrequentToy()).getCount());
            toyList.remove(getThatToy(getMostFrequentToy()));
            count++;
        }

        toyList = t;
    }

    public String toString() {
        return toyList + "" + "\n" + "max == " + getMostFrequentToy();
    }
}

这是我关心的方法

public void sortToysByCount() {
    ArrayList<Toy> t = new ArrayList<Toy>();
    int count = 0;
    int size = toyList.size();

    for (int i = size; i > 0; i--) {
        t.add(new Toy(getMostFrequentToy()));
        t.get(count).setCount(getThatToy(getMostFrequentToy()).getCount());
        toyList.remove(getThatToy(getMostFrequentToy()));
        count++;
    }

    toyList = t;
}

这是我的输出

  [sorry 4, bat 1, train 2, teddy 2, ball 2]

这是我想要的

  [sorry 4, train 2, teddy 2, ball 2, bat 1];

我的代码有什么问题?我该怎么做


共 (2) 个答案

  1. # 1 楼答案

    您的代码有一些效率。每次调用getMostFrequentToy()时,您都会在整个列表中进行迭代,这可能很好,因为您一直在删除对象,但实际上不需要为列表中已经存在的对象创建new Toy对象

    因此,这是“更好的”,但仍然不确定您是否需要getThatToy当您应该已经知道哪一个是最常见的

    String frequent;
    for (int i = size; i > 0; i ) {
        frequent = getMostFrequentToy();
        t.add(new Toy(frequent));
        t.get(count).setCount(getThatToy(frequent).getCount());
        toyList.remove(getThatToy(frequent));
        count++;
    }
    

    无论如何,我想说明书要求你归还玩具,而不是它的名字

    这很简单,只需跟踪最大计数

    public Toy getMostFrequentToy() {
        Toy mostFrequent = null;
        int maximum = Integer.MIN_VALUE;
    
        for (Toy t : toyList) {
            if (t.getCount() > maximum)
                mostFrequent = t;
        }
        return t;
    }
    

    现在,上面的代码可以变成

    public void sortToysByCount() {
        ArrayList<Toy> t = new ArrayList<Toy>();
        // int count = 0;
        int size = toyList.size();
    
        Toy frequent;
        for (int i = size; i > 0; i ) {
            frequent = getMostFrequentToy();
            t.add(frequent);
            // t.get(count).setCount(frequent.getCount()); // Not sure about this
            toyList.remove(frequent);
            // count++;
        }
    
        toyList.clear();
        toyList.addAll(t);
    }
    

    但实际上,当您想要排序时,您确实应该了解如何create a ^{} for your ^{} objects

  2. # 2 楼答案

    问题出在getMostFrequentToy()方法中:

    替换

            if (toyList.get(i).getCount() > maximum)
                maximum = toyList.get(i).getCount();
            position = i;
    

            if (toyList.get(i).getCount() > maximum) {
                maximum = toyList.get(i).getCount();
                position = i;
            }
    

    因为你想得到对应于最大值的位置