2015-12-30 133 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 
}