【问题标题】:Find max value of an arraylist of objects查找对象数组列表的最大值
【发布时间】:2021-08-15 11:23:33
【问题描述】:

假设我已经有一个名为 Motorbike 的课程,其中有一个 getWeight() 我不知道为什么我的代码不起作用,这是为了测试,我没有示例文本

public static ArrayList<Motorbike> maxWeight (ArrayList<Motorbike> motorbikes){
    ArrayList<Motorbike> max_list = new ArrayList<Motorbike>();
    Motorbike max = motorbikes.get(0);
    for (int i = 0; i < motorbikes.size(); i++){
        if (motorbikes.get(i).getWeight() > max.getWeight(){
            max = motorbikes(get(i));
            max_list.add(max);
        }
    }
    return max_list;
}

这是摩托车类:

public class Motorbike
{
    private String id;
    private String name;
    private String manu;
    private int quantity;
    private double price;
    private double weight;
    private String date;
    
    public Motorbike(String id,String name,String manu,int quantity,double price,double weight,String date){
        this.id = id;
        this.name = name;
        this.manu = manu;
        this.quantity = quantity;
        this.price = price;
        this.weight = weight;
        this.date = date;
    }
    
    public String getId(){
        return id;
    }
    public String getName(){
        return name;
    }
    public String getManu(){
        return manu;
    }
    public int getQuantity(){
        return quantity;
    }
    public double getPrice(){
        return price;
    }
    public double getWeight(){
        return weight;
    }
    public String getDate(){
        return date;
    }
    
    void printMotorbike(){
        System.out.println(getId() + " " + getName() + " " + getManu() + " " + getQuantity() + " " + getPrice() + " " + getWeight() + " " + getDate());
    }
}

错误是Index 0 out of bounds for length 0 我提交了我的代码,这就是我能提供的全部内容

【问题讨论】:

  • '摩托车最大值 = motorbikes.get(i);' 'i' 在哪里声明?
  • 您想要 ONE max 摩托车的重量吗?该方法不应返回列表
  • 我修复了那部分,方法已经为我们编写好了,我们只能添加我们的代码
  • 请提供一个可重现的示例,其中包含要测试的摩托车定义和示例代码。还有 epxlain 如何不做你想做的事
  • 为了将来参考,当您在 stackoverflow 上发布您的下一个问题时:如果您的 [java] 代码抛出异常,请发布整个堆栈跟踪,而不仅仅是错误信息。

标签: java arraylist max


【解决方案1】:

最大值是一个,不是列表,所以忘记使用max_list。

尝试使用流的max 方法:

Motorbike max = motorbikes.stream()
  .max(comparing(Motorbike::getWeight))
  .orElse(null);

【讨论】:

    【解决方案2】:

    问题

    • 错误Index 0 out of bounds for length 0表示提供的ArrayList&lt;Motorbike&gt; motorbikes空的所以你可以在之前添加一个检查
    • 最大重量的摩托车是一辆摩托车,所以你的方法应该返回一个元素而不是List&lt;Motorbike&gt;

    解决方案

    使用 for 循环,你可能会有类似的东西

    static Motorbike maxWeight(List<Motorbike> motorbikes) {
        if (motorbikes.isEmpty())
            return null;
        Motorbike max = motorbikes.get(0);
        for (Motorbike m : motorbikes) {
            if (m.getWeight() > max.getWeight()) {
                max = m;
            }
        }
        return max;
    }
    

    使用 Streams,您可以拥有

    static Motorbike maxWeight(List<Motorbike> motorbikes) {
        return motorbikes.stream().max(Comparator.comparing(Motorbike::getWeight)).orElse(null);
    }
    

    【讨论】:

      【解决方案3】:

      我了解列表中可能有不止一辆摩托车具有最大重量,方法maxWeight 需要返回所有具有最大重量的摩托车。

      首先你需要找出最大重量是多少。

      找到最大重量后,您需要再次遍历列表并提取所有重量等于您找到的最大重量的摩托车。

      因此您需要遍历列表两次。

      public static ArrayList<Motorbike> maxWeight(ArrayList<Motorbike> motorbikes){
          ArrayList<Motorbike> maxList = new ArrayList<Motorbike>();
      
          // Find maximum weight.
          Motorbike max = motorbikes.get(0);
          for (int i = 0; i < motorbikes.size(); i++){
              if (motorbikes.get(i).getWeight() > max.getWeight(){
                  max = motorbikes.get(i);
              }
          }
      
          // Find all motorbikes in list having maximum weight.
          for (Motorbike bike : motorbikes) {
              if (bike.getWeight() == max.getWeight()) {
                  maxList.add(bike);
              }
          }
          return maxList;
      }
      

      【讨论】:

        猜你喜欢
        • 2018-09-13
        • 1970-01-01
        • 2015-09-29
        • 2019-08-01
        • 1970-01-01
        • 2015-03-15
        • 2018-04-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多