用java打印arraylist

用java打印arraylist,java,arraylist,Java,Arraylist,实际上这是一个arrayList,我想打印出来。但每次我都会出错。有人请帮帮我。class产品 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"

实际上这是一个arrayList,我想打印出来。但每次我都会出错。有人请帮帮我。
class产品

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"));
但它不起作用。有人请帮忙。 注意:Main方法也是类产品的一部分


提前谢谢

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

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

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

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

要使其正确工作,还需要重写Product类中的方法。

请明确产品列表和单个产品。为了清晰起见,我将列表重命名为productList

    static List<Product> productList = new ArrayList<Product>();
您需要重写
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);
    }
}
公共类产品{
静态列表productList=新的ArrayList();
字符串类型;
弦材料;
字符串颜色;
int-n1;
int n2;
静态void init(){
产品列表。添加(新产品(“裤子”、40499、“棉质”、“黑色”);
产品列表。添加(新产品(“衬衫”,32999,“棉质”,“白色”));
产品列表。添加(新产品(“T恤”,32599,“棉质”,“蓝色”);
产品列表。添加(新产品(“Blazer”,321299,“棉质”,“棕色”);
}
产品(字符串类型、整数n1、整数n2、字符串材质、字符串颜色){
this.type=type;
这1.n1=n1;
这1.n2=n2;
这个材料=材料;
这个颜色=颜色;
}
公共静态void main(字符串[]ar)
{
init();
对于(产品pd:productList){
系统输出打印项次(pd);
}
}
@凌驾
公共字符串toString(){
退货(类型+“”+n1+“”+n2+“”+物料+“”+颜色);
}
}

您应该使用
getter/setter
访问单个成员变量(String和int),或者直接从产品实例调用成员变量(如果声明为public),如

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

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


若要迭代列表,可以使用迭代器或增强的for循环(针对每个循环)。 所以你可以根据你的要求使用任何一种 这里我使用了增强的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
}
List pd=new ArrayList();
pd.添加(新产品(“裤子”、40499、“棉质”、“黑色”);
pd.添加(新产品(“衬衫”,32999,“棉质”,“白色”));
pd.添加(新产品(“T恤”,32599,“棉质”,“蓝色”);
pd.添加(新产品(“Blazer”,321299,“棉质”、“棕色”);
对于上面的列表,您可以使用下面的列表
适用于(产品专业:pd)
{
//在这里你可以一个接一个地得到价值
System.out.println(pro.toString());//它将打印列表的所有值
}

您可能没有在
产品
类中重写
toString
。多次声明
pd
变量也无济于事。不工作是什么意思?任何异常?我不能同时打印字符串和整数吗?你说的
不能同时打印字符串和整数是什么意思?
系统.out.println(pd.getString()+“”+pd.getInteger())确定。我已更改对象名称。但仍然有错误。@Darpanjbora:请更新您的代码。请注意,仅仅更改名称不会有太大的区别,因为问题是迭代对象的类型。好的,我已经更改了对象名称。但是列表有什么问题吗?你需要在main方法中有这个列表。我猜你正在产品类的列表中添加元素,你应该在main中这样做,然后重复我在答案中所做的。非常感谢。其工作原理如下:顺便说一句,如果我需要使用个别属性,如产品名称或大小。。。我可以只通过pd.name或pd.n1访问它吗???@Darpanjbora为什么不先尝试一下呢?@AadityaGavandalkar:当然可以。:)是 啊可以通过这种方式访问。:)您需要了解访问控制。如果将实例变量声明为公共变量,则可以在任何地方访问它们,如
pd.color
。更常见的是有getter和setter,它们是中介对对象内部的访问的方法。变量还可以具有私有、受保护或默认访问权限,这在许多Java教程中都有描述。
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);
    }
}
Product pd;
pd.getType();pd.getCount(); 
pd.type;pd.count;//only if member variables are public
@Override
    public String toString() {
        return(type+" "+count+" "+price+" "+material+" "+color);
    }
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
}