如何打印java中arraylist内的pojo对象的值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18764949/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 10:50:59  来源:igfitidea点击:

How to print the values of the pojo object which is inside an arraylist in java?

javaarraylistpojo

提问by sTg

I have a requirement where i have to print the values which are getting saved in a database. I have pojo object which i am passing it in a list and then saving the entire list in database. Please find the below code.

我有一个要求,我必须打印保存在数据库中的值。我有 pojo 对象,我将它传递到列表中,然后将整个列表保存在数据库中。请找到以下代码。

List dataList=new ArrayList();
Vehicles vehiclePojoObject=new Vehicles();
vehiclePojoObject.setName("Cycle");
vehiclePojoObject.setColor("Blue");
dataList.add(vehiclePojoObject);

Now i have to print the values which is contained by vehiclePojoObject. How can it be acheived. Please guide. Thanks in advance.

现在我必须打印 VehiclePojoObject 包含的值。怎么可能达到。请指导。提前致谢。

Note: There are not one object but multiple objects which are getting stored in the list.

注意:列表中存储的不是一个对象而是多个对象。

采纳答案by McMonster

Assuming that you want to store objects of different types on the same list and have a single method for printing all private fields of those object regardless of their types you can use the (reflection API). By the help of reflection we can change behavior of list at runtime

假设您想将不同类型的对象存储在同一个列表中,并且有一个方法来打印这些对象的所有私有字段,而不管它们的类型如何,您都可以使用 (reflection API)。通过反射的帮助,我们可以在运行时改变列表的行为

回答by Naveen Kumar Alonekar

Add vehiclePojoObjectto the Vehicles List object, like

添加vehiclePojoObject到 Vehicles List 对象,例如

List<Vehicles> vehList = new ArrayList<Vehicles>();
Vehicles vehiclePojoObject=new Vehicles();
vehiclePojoObject.setName("Cycle");
vehiclePojoObject.setColor("Blue");
vehList.add(vehiclePojoObject); //Here we are adding pojoObject to list object

And get Vehicles List data through for-each

并通过获取车辆列表数据 for-each

i.e.

IE

for(Vehicles vehicle : vehList){
  System.out.println("Vehicle Name: "+veh.getName());
  System.out.println("Vehicle Color: "+veh.getColor());
}

回答by npinti

Other than the solution posted by @McMonster, you could also make sure that your POJO's override the toString()method so that you can print a customized, and most likely, more readable string representation of your object.

除了@McMonster 发布的解决方案之外,您还可以确保您的 POJO 覆盖该toString()方法,以便您可以打印自定义的、最有可能的、更具可读性的对象字符串表示形式。

回答by Ankit Nidhi

List dataList = new ArrayList<>();

List dataList = new ArrayList<>();

    // Print the name from the list....
    for(Vehicles vehicle: Vehicles) {
        System.out.println(vehicle.getName());
        System.out.println(vehicle.getColor());
    }

Hope this helps!!!

希望这可以帮助!!!

回答by drees

Expanding on @npinti answer of overriding toString()

扩展覆盖 toString() 的 @npinti 答案

In your POJO file, add this function:

在您的 POJO 文件中,添加此函数:

@Override
public String toString() {
    String output = "Name: " + getName() + " Color: " + getColor +
        " Model: " + getModel() + ....;

    return output;
}

then you can loop through your list to and call .toString() on all the objects to print out all of the features

然后您可以遍历您的列表并在所有对象上调用 .toString() 以打印出所有功能

for(Vehicles vehicle : vehList){
     System.out.println(vehicle.toString());
}