【问题标题】:Sort objects highest-lowest in Java with ArrayList?使用ArrayList对Java中的对象进行排序最高到最低?
【发布时间】:2017-03-25 02:57:36
【问题描述】:

编写一个方法来返回列表中出现频率最高的玩具,并编写另一个方法来按数量对玩具进行排序。

这是我的代码

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];

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

【问题讨论】:

  • 使用调试器逐步检查您的代码并找出答案。家庭作业是通过反复试验来教的,如果我们为你做作业有什么意义?
  • 它看起来像一个家庭作业。这是我们的政策about homeworks

标签: java sorting arraylist methods


【解决方案1】:

问题出在您的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;
        }

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

【讨论】:

    【解决方案2】:

    您的代码效率低下。每次调用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 Comparator for your Toy objects

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-30
      • 2023-03-11
      • 2014-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-12
      相关资源
      最近更新 更多