2015-12-30 129 views
-2

其实这里是一个arrayList,我希望它打印。但每当我收到错误。有人请帮助我。 class Product在java中打印arraylist

List<Product> pd = new ArrayList<Product>(); 
pd.add(new Product("Trouser",40, 499,"Cotton", "Black")); 
pd.add(new Product("Shirt",32, 999, "Cotton","White")); 
pd.add(new Product("T-Shirt",32, 599 ,"Cotton","Blue")); 
pd.add(new Product("Blazer",32, 1299 ,"Cotton","Brown")); 

所以这是ArrayList的..我要打印出来。 我试过这种方式 -

public static void main(String [] ar) 
{ 
Product pd = new product(); 
for(Object o : pd) 
    { 
    Product pd = (Product)o; 
    System.out.println(pd); 
    } 

但它不工作。有人请帮忙。 注:主要方法也是类Product的一部分。

在此先感谢!

+2

。而且它无助于你多次声明'pd'变量。 – Eran

+0

不工作是什么意思?有什么异常? – xyz

+0

我不能一起打印字符串和整数吗? – Darpanjbora

回答

1

明确产品清单和个别产品。在这里,我已经改名为列表被PRODUCTLIST为清楚起见

static List<Product> productList = new ArrayList<Product>(); 

当翻翻目录,没有铸造需要

 for(Product pd : productList) { 
      System.out.println(pd); 
     } 
    } 

你需要重写toString方法来获得您所需要

输出
@Override 
    public String toString() { 
     return(type + " " + n1 + " " + n2 + " " + material + " " + color); 
    } 
} 

全部放在一起 -

public class Product { 
    static List<Product> productList = new ArrayList<Product>(); 
    String type; 
    String material; 
    String color; 
    int n1; 
    int n2; 

    static void init() { 
     productList.add(new Product("Trouser",40, 499,"Cotton", "Black")); 
     productList.add(new Product("Shirt",32, 999, "Cotton","White")); 
     productList.add(new Product("T-Shirt",32, 599 ,"Cotton","Blue")); 
     productList.add(new Product("Blazer",32, 1299 ,"Cotton","Brown")); 
    } 

    Product (String type, int n1, int n2, String material, String color) { 
     this.type = type; 
     this.n1 = n1; 
     this.n2 = n2; 
     this.material = material; 
     this.color = color; 
    } 
    public static void main(String [] ar) 
    { 
     init(); 
     for(Product pd : productList) { 
      System.out.println(pd); 
     } 
    } 
    @Override 
    public String toString() { 
     return(type + " " + n1 + " " + n2 + " " + material + " " + color); 
    } 
} 
+0

非常感谢。它的工作。 :)顺便说一句,如果我需要使用个人属性,如产品名称或大小......我可以通过pd.name或pd.n1访问它? – Darpanjbora

+1

@Darpanjbora你为什么不先试试? –

+0

@AadityaGavandalkar:是的。 :) – Darpanjbora

0

你需要记住的是,pd你在这里宣布:Product pd = new product();pd您声明此不同:

List<Product> pd = new ArrayList<Product>(); 
pd.add(new Product("Trouser",40, 499,"Cotton", "Black")); 
pd.add(new Product("Shirt",32, 999, "Cotton","White")); 
pd.add(new Product("T-Shirt",32, 599 ,"Cotton","Blue")); 
pd.add(new Product("Blazer",32, 1299 ,"Cotton","Brown")); 

一个是Product对象(后者)的列表的一个实例而前者是Product类本身的一个实例。

因此,试图执行此操作:for(Object o : pd)将导致错误,除非Product实现了Iterable接口。

要解决这个问题,你需要做一些事情,像这样:

List<Product> pd = new ArrayList<Product>(); 
pd.add(new Product("Trouser",40, 499,"Cotton", "Black")); 
pd.add(new Product("Shirt",32, 999, "Cotton","White")); 
pd.add(new Product("T-Shirt",32, 599 ,"Cotton","Blue")); 
pd.add(new Product("Blazer",32, 1299 ,"Cotton","Brown")); 

for(Product p : pd) { 
    System.out.println(...); 
} 
+0

好的。我已经改变了对象名称。但仍然出现错误。 – Darpanjbora

+0

@Darpanjbora:请更新您的代码。请注意,只是更改名称不会有太大的区别,因为问题是您正在迭代的对象的* type *。 – npinti

0

你是不是正确遍历列表再加上有一个名为pd太多的变数。

假设pd是包含所有产品对象列表,

for(Product p : pd) 
{ 
    System.out.println(p); 
} 

为了这个正常工作,你还需要覆盖产品类toString()方法。

+0

好的,我已经改变了对象名称。但是列表有什么问题? – Darpanjbora

+0

你需要在main方法中有这个列表。我猜你是在Product类的列表中添加元素,你应该在main中做,然后迭代我在答案中完成的。 –

+0

好的。非常感谢帮助我。 – Darpanjbora

1

您应该使用getters/setters访问单个成员变量(字符串和INT),或直接从产品实例调用成员变量(如声明为public)像

Product pd; 
pd.getType();pd.getCount(); 

pd.type;pd.count;//only if member variables are public 

如果您只想打印所有产品成员变量;重写toString()方法。

@Override 
    public String toString() { 
     return(type+" "+count+" "+price+" "+material+" "+color); 
    } 
0

如果要迭代列表,可以使用Iterator或增强for循环(for each)。 所以你可以使用基于您的需求量的你可能没有在`Product`类重写`toString`任何一个 这里我用增强的for循环

List<Product> pd = new ArrayList<Product>(); 
pd.add(new Product("Trouser",40, 499,"Cotton", "Black")); 
pd.add(new Product("Shirt",32, 999, "Cotton","White")); 
pd.add(new Product("T-Shirt",32, 599 ,"Cotton","Blue")); 
pd.add(new Product("Blazer",32, 1299 ,"Cotton","Brown")); 

for the above list you can use the below one 

for(Product pro : pd) 
{ 
// here you can get value one by one 
System.out.println(pro.toString()); // it will print all the values of list 
}