如何从具有不同类型元素的arraylist中获取特定值?

时间:2020-07-06 13:29:12

标签: java arraylist element

请帮助我,我可以理解如何仅获取元素的值之一并与其他类型的元素进行相同类型的收集,而我需要权衡价格和价格。

    DecimalFormat df = new DecimalFormat("0.00");

    Products p1 = new Products(1, "tomatoes-", 90, 75, 45, 0.0, 0.98);
    Products p2 = new Products(2, "potatoes-", 80, 54, 35, 8.90, 0.67);
    Products p3 = new Products(3, "melon-", 123, 60, 100, 0.08, 0.70);
    Products p4 = new Products(4, "eggplant-", 30, 50, 56, 4.78, 0.60);

    ArrayList<Products> product = new ArrayList<Products>();
    product.add(p1);
    product.add(p2);
    product.add(p3);
    product.add(p4);

    Iterator itr = product.iterator();
     while (itr.hasNext()) {
        Products pt = (Products) itr.next();

          //sum of the weight if product weight is more than 100g 
            if (pt.weight > 0.1) {
             double sumWeight =+pt.weight ;
             System.out.println(sumWeight);
            }

            //show products
            System.out.printf("id: " + pt.id + " name: " + pt.name + " " + pt.height + "x" +pt.lenght 
            + "x"+ pt.width + "mm" + " weight: " + df.format(pt.weight) + "kg" + " price: " 
            +df.format(pt.price)+ "lv.\n");

        }
    }
}

class Products {

    int id;
    String name;
    int height;
    int lenght;
    int width;
    double weight;
    double price;

    public Products(int id, String name, int height, int lenght, int width, double weight, double price) 
    {
        this.id = id;
        this.name = name;
        this.height = height;
        this.lenght = lenght;
        this.width = width;
        this.weight = weight;
        this.price = price;
    }
  }

2 个答案:

答案 0 :(得分:1)

这有帮助吗?

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

class Products {
    int id;
    String name;
    int height;
    int lenght;
    int width;
    double weight;
    double price;
    DecimalFormat df;

    public Products(int id, String name, int height, int lenght, int width, double weight, double price) {
        this.id = id;
        this.name = name;
        this.height = height;
        this.lenght = lenght;
        this.width = width;
        this.weight = weight;
        this.price = price;
        this.df = new DecimalFormat("0.00");
    }

    @Override
    public String toString() {
        return "id: " + this.id + " name: " + this.name + " " + this.height + "x" + this.lenght
                + "x" + this.width + "mm" + " weight: " + df.format(this.weight) + "kg" + " price: "
                + df.format(this.price) + "lv.";
    }
}

class Scratch {
    public static void main(String[] args) {
        Products p1 = new Products(1, "tomatoes-", 90, 75, 45, 0.0, 0.98);
        Products p2 = new Products(2, "potatoes-", 80, 54, 35, 8.90, 0.67);
        Products p3 = new Products(3, "melon-", 123, 60, 100, 0.08, 0.70);
        Products p4 = new Products(4, "eggplant-", 30, 50, 56, 4.78, 0.60);

        List<Products> product = new ArrayList<>();
        product.add(p1);
        product.add(p2);
        product.add(p3);
        product.add(p4);

        double sumWeight = 0;
        double sumPrice = 0;
        for (Products pt : product) {
            //sum of the weight if product weight is more than 100g
            if (pt.weight > 0.1) {
                sumWeight += pt.weight;
                sumPrice += pt.price;
            }

            //show products
            System.out.println(pt);
        }

        System.out.println("\nAggregated attribute ---");
        System.out.println("Total Wight: " + sumWeight);
        System.out.println("Total Price: " + sumPrice);
    }
}

注意:实际上,您不需要迭代器即可遍历列表。如果您要格式化课程,也请使用toString()

答案 1 :(得分:1)

您需要解决的问题是:

  • sumWeight必须在循环外部定义。否则,它将无法累积几种产品的重量。
  • sumPrice的定义必须类似
  • sumWeight=+pt.weight不会为总和添加新的权重。您可能已经将=++=运算符混淆了。您可以使用+=运算符来执行此操作,但通常更容易将其完全拼写并编写sumWeight = sumWeight + pt.weight。这样就不会造成混乱。

这是最小但有效的代码:

    double sumWeight = 0;
    double sumPrice = 0;
    for (Products p : product) {
        if (p.weight > 0.1) {
            sumWeight = sumWeight + p.weight;
            sumPrice = sumPrice + p.price;
        }
    }
    System.out.println("Total weight=" + sumWeight);
    System.out.println("Total price=" + sumPrice);