Java Arraylist两个不同的列
我有一个带有项目名称和成本的数组列表,但是,我想将两者分开,特别是这样我可以对成本值求和 以下是我到目前为止的情况:Java Arraylist两个不同的列,java,arrays,arraylist,items,Java,Arrays,Arraylist,Items,我有一个带有项目名称和成本的数组列表,但是,我想将两者分开,特别是这样我可以对成本值求和 以下是我到目前为止的情况: import java.util.ArrayList; public class henrys { public static ArrayList<String> products = new ArrayList<String>(); public static void main(String[] args) { p
import java.util.ArrayList;
public class henrys {
public static ArrayList<String> products = new ArrayList<String>();
public static void main(String[] args) {
products.add("Calvin Klein Blue Shorts, Size: Medium, $45");
products.add("Ralph Lauren Blue Shirt, Size: Large, $50");
products.add("Gap Beige Khakis, Size: 30, $20");
products.add("Calvin Klein Black Shirt, Size: Medium, $35");
products.add("USA Jersey White, Size: Medium, $100");
System.out.println((products));
}
}
import java.util.ArrayList;
公营亨利酒店{
public static ArrayList products=new ArrayList();
公共静态void main(字符串[]args){
添加(“Calvin Klein蓝色短裤,尺码:中等,45美元”);
产品。添加(“拉尔夫·劳伦蓝色衬衫,尺码:大号,50美元”);
添加(“Gap米色卡其裤,尺码:30,$20”);
添加(“Calvin Klein黑色衬衫,尺码:中等,35美元”);
产品。添加(“美国针织白色,尺码:中等,$100”);
System.out.println((产品));
}
}
像这样创建ArrayList真的没有意义,完全违背了以对象为中心的语言的目的
创建一个产品类,给它一个名称
、价格
和大小
字段,然后创建一个产品对象的ArrayList
要获得价格之和,您可以迭代ArrayList
中的每个产品
,并添加其价格:
旁注:将货币价值存储为双精度是一个可怕的想法。我只是想告诉你如何开始
最终编辑
您需要使用以下代码创建一个名为Product
的类:
public class Product {
public String name, size;
public double price;
public Product(String name, String size, double price){
this.name = name;
this.size = size;
this.price = price;
}
}
在您的Main
课程中:
public static void main(String[] args){
ArrayList<Product> products = new ArrayList<Product>();
products.add(new Product("Shorts", "Small", 5.45);
products.add(new Product("Shirt", "Large", 15.15);
products.add(new Product("Hat", "Small", 10.25);
//Calculate total price.
double price = 0.00;
for(Product p : products){
price += p.price;
}
System.out.println("Total cost: " + price);
}
publicstaticvoidmain(字符串[]args){
ArrayList产品=新的ArrayList();
产品。添加(新产品(“短裤”,“小”,5.45);
添加(新产品(“衬衫”、“大号”,15.15);
添加(新产品(“帽子”、“小”,10.25);
//计算总价格。
双倍价格=0.00;
对于(产品p:产品){
价格+=p.价格;
}
系统输出打印项次(“总成本:+价格);
}
像这样创建ArrayList真的没有意义,完全违背了以对象为中心的语言的目的
创建一个产品类,给它一个名称
、价格
和大小
字段,然后创建一个产品对象的ArrayList
要获得价格之和,您可以迭代ArrayList
中的每个产品
,并添加其价格:
旁注:将货币价值存储为双倍是一个可怕的想法。我这样做只是为了告诉你如何开始
最终编辑
您需要使用以下代码创建一个名为Product
的类:
public class Product {
public String name, size;
public double price;
public Product(String name, String size, double price){
this.name = name;
this.size = size;
this.price = price;
}
}
在您的Main
课程中:
public static void main(String[] args){
ArrayList<Product> products = new ArrayList<Product>();
products.add(new Product("Shorts", "Small", 5.45);
products.add(new Product("Shirt", "Large", 15.15);
products.add(new Product("Hat", "Small", 10.25);
//Calculate total price.
double price = 0.00;
for(Product p : products){
price += p.price;
}
System.out.println("Total cost: " + price);
}
publicstaticvoidmain(字符串[]args){
ArrayList产品=新的ArrayList();
产品。添加(新产品(“短裤”,“小”,5.45);
添加(新产品(“衬衫”、“大号”,15.15);
添加(新产品(“帽子”、“小”,10.25);
//计算总价格。
双倍价格=0.00;
对于(产品p:产品){
价格+=p.价格;
}
系统输出打印项次(“总成本:+价格);
}
像这样创建ArrayList真的没有意义,完全违背了以对象为中心的语言的目的
创建一个产品类,给它一个名称
、价格
和大小
字段,然后创建一个产品对象的ArrayList
要获得价格之和,您可以迭代ArrayList
中的每个产品
,并添加其价格:
旁注:将货币价值存储为双倍是一个可怕的想法。我这样做只是为了告诉你如何开始
最终编辑
您需要使用以下代码创建一个名为Product
的类:
public class Product {
public String name, size;
public double price;
public Product(String name, String size, double price){
this.name = name;
this.size = size;
this.price = price;
}
}
在您的Main
课程中:
public static void main(String[] args){
ArrayList<Product> products = new ArrayList<Product>();
products.add(new Product("Shorts", "Small", 5.45);
products.add(new Product("Shirt", "Large", 15.15);
products.add(new Product("Hat", "Small", 10.25);
//Calculate total price.
double price = 0.00;
for(Product p : products){
price += p.price;
}
System.out.println("Total cost: " + price);
}
publicstaticvoidmain(字符串[]args){
ArrayList产品=新的ArrayList();
产品。添加(新产品(“短裤”,“小”,5.45);
添加(新产品(“衬衫”、“大号”,15.15);
添加(新产品(“帽子”、“小”,10.25);
//计算总价格。
双倍价格=0.00;
对于(产品p:产品){
价格+=p.价格;
}
系统输出打印项次(“总成本:+价格);
}
像这样创建ArrayList真的没有意义,完全违背了以对象为中心的语言的目的
创建一个产品类,给它一个名称
、价格
和大小
字段,然后创建一个产品对象的ArrayList
要获得价格之和,您可以迭代ArrayList
中的每个产品
,并添加其价格:
旁注:将货币价值存储为双倍是一个可怕的想法。我这样做只是为了告诉你如何开始
最终编辑
您需要使用以下代码创建一个名为Product
的类:
public class Product {
public String name, size;
public double price;
public Product(String name, String size, double price){
this.name = name;
this.size = size;
this.price = price;
}
}
在您的Main
课程中:
public static void main(String[] args){
ArrayList<Product> products = new ArrayList<Product>();
products.add(new Product("Shorts", "Small", 5.45);
products.add(new Product("Shirt", "Large", 15.15);
products.add(new Product("Hat", "Small", 10.25);
//Calculate total price.
double price = 0.00;
for(Product p : products){
price += p.price;
}
System.out.println("Total cost: " + price);
}
publicstaticvoidmain(字符串[]args){
ArrayList产品=新的ArrayList();
产品。添加(新产品(“短裤”,“小”,5.45);
添加(新产品(“衬衫”、“大号”,15.15);
添加(新产品(“帽子”、“小”,10.25);
//计算总价格。
双倍价格=0.00;
对于(产品p:产品){
价格+=p.价格;
}
系统输出打印项次(“总成本:+价格);
}
您是否考虑过地图
?使用ArrayListinstead@Bharath或者a。不要将产品表示为字符串;创建一个产品类,该类包含您所需的所有不同属性和适当的类型。@ElliottFrisch是的,如果他想维持订单,他可以使用LinkedHashMap。您是否考虑过映射
?使用ArrayLisTinstead@Bharath或者a。不要将产品表示为字符串;创建一个产品类,该类包含您所需的所有各种属性和适当的类型。@ElliottFrisch是的,如果他想维持订单,他可以使用LinkedHashMap。您是否考虑过映射
?使用ArrayListinstead@Bharath或者一个。不要再报告了