Java 为ArrayList中的每个对象打印不同的单词
我有多个客户端对象。每个客户端对象都有一个名为shoppingCart的ArrayList。这些ArrayList由我创建的Product类的对象填充。这些产品可以是一流的衬衫、牛仔裤或裙子(都是继承产品)。 我想把每个客户在购物车上的东西打印成字符串。例如,如果客户的shoppingCart中有一件衬衫和一件裙子对象,控制台将打印:“购物车的内容:衬衫,裙子”Java 为ArrayList中的每个对象打印不同的单词,java,string,arraylist,Java,String,Arraylist,我有多个客户端对象。每个客户端对象都有一个名为shoppingCart的ArrayList。这些ArrayList由我创建的Product类的对象填充。这些产品可以是一流的衬衫、牛仔裤或裙子(都是继承产品)。 我想把每个客户在购物车上的东西打印成字符串。例如,如果客户的shoppingCart中有一件衬衫和一件裙子对象,控制台将打印:“购物车的内容:衬衫,裙子” 我怎样才能做到这一点呢?你可以这样做。多态性的一个例子是: 当您在购物车上迭代并打印类型时,您会得到相应的类型 for(Product
我怎样才能做到这一点呢?你可以这样做。多态性的一个例子是: 当您在
购物车上迭代并打印类型时,您会得到相应的类型
for(Product p : shoppingCart) {
System.out.println(p.getType);
}
我认为这可以通过使用运算符来实现。您可以尝试这样做:
public List getContentsOfCart(列出产品){
列表结果=新建ArrayList();
对于(产品p:产品){
如果(裙板的p实例){
结果。添加(“裙子”);
}else if(衬衫的p实例){
结果。添加(“衬衫”);
}else if(p instancef牛仔裤){
结果。添加(“牛仔裤”);
}
}
返回结果;
}
然后,您可以按如下方式打印此列表:
System.out.println(“购物车的内容:+Strings.join(结果,”));
假设产品列表是产品
,您可以按如下方式进行操作:
List<String> productNames = new ArrayList<>();
for (Product p : products) {
productNames.add(p.getName());// Assuming Product#getName returns product name
}
// Join the product names and display
System.out.println("Contents of cart: " + String.join(",", productNames));
List products=new ArrayList();
对于(产品p:产品){
productNames.add(p.getName());//假设产品#getName返回产品名称
}
//加入产品名称和显示
System.out.println(“购物车的内容:+String.join(“,”,productNames));
示例代码:
public enum ProductType {
PANT,
SHIRT,
SKIRT,
TSHIRT,
}
public class Product {
private ProductType productType;
public Product( ProductType productType) {
this.productType = productType;
}
public ProductType getProductType() {
return productType;
}
}
public class Pant extends Product {
private int size;
public Pant(ProductType productType, int size) {
super(productType);
this.size = size;
}
}
public class Shirt extends Product {
private int size;
public Shirt(ProductType productType, int size) {
super(productType);
this.size = size;
}
}
public class App {
public static void main(String[] args) {
List<Product> cart = List.of(new Pant(ProductType.PANT, 100),
new Pant(ProductType.PANT, 101),
new Shirt(ProductType.SHIRT, 42));
System.out.println("Contents of cart: " +
cart.stream()
.map(Product::getProductType)
.collect(Collectors.toList()));
}
}
这回答了你的问题吗?请告诉我们您遇到问题的代码。这不是一个家庭作业写作服务,但我们在这里帮助解决特定的编程问题。
public enum ProductType {
PANT,
SHIRT,
SKIRT,
TSHIRT,
}
public class Product {
private ProductType productType;
public Product( ProductType productType) {
this.productType = productType;
}
public ProductType getProductType() {
return productType;
}
}
public class Pant extends Product {
private int size;
public Pant(ProductType productType, int size) {
super(productType);
this.size = size;
}
}
public class Shirt extends Product {
private int size;
public Shirt(ProductType productType, int size) {
super(productType);
this.size = size;
}
}
public class App {
public static void main(String[] args) {
List<Product> cart = List.of(new Pant(ProductType.PANT, 100),
new Pant(ProductType.PANT, 101),
new Shirt(ProductType.SHIRT, 42));
System.out.println("Contents of cart: " +
cart.stream()
.map(Product::getProductType)
.collect(Collectors.toList()));
}
}
Contents of cart: [PANT, PANT, SHIRT]