在java中打印arraylist

时间:2015-12-30 09:39:46

标签: java arraylist

实际上这里是一个arrayList,我想要它打印。但每次我都会收到错误。有人请帮助我。class Product

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);
    }

但它不起作用。有人请帮忙。 注意:主要方法也是产品类的一部分。

提前致谢!!

5 个答案:

答案 0 :(得分: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);
    }
}

答案 1 :(得分:1)

您应该使用getters/setters来访问单个成员变量(String和int),或者直接从Product实例调用成员变量(如果声明为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);
    }

答案 2 :(得分:0)

您需要记住,此处声明的pdProduct 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(...);
}

答案 3 :(得分:0)

您没有正确地迭代列表,而且有太多名为pd的变量。

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

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

为了使其正常工作,您还需要覆盖Product类中的toString()方法。

答案 4 :(得分:0)

如果要迭代列表,可以使用Iterator或增强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
}