简体   繁体   中英

Printing a list of objects only prints their string value

I want to make a lists of objects in Java but when I print the list it contains only the string value:

List<Bucketreturn> list = new ArrayList<Bucketreturn>();            

Bucketreturn br=new Bucketreturn();//creates bucket objects
br.bucket_id=1004;
br.bucket_name=java;
br.item_number=4;
br.date_bucket_added="02-06-2012";
list.add(br);
System.out.println(list);
// prints only string value but I want the integer should also be printed

You should override Bucketreturn.toString() and let it return the exact string you want to see.

Something like

  public String toString() {
     return date_bucket_added + " " + bucket_id + " "  + bucket_name + " " + item_number;
   }

If you use Eclipse it has features for helping you easily maintain your toString() methods.

Override the toString() method in your Bucketreturn class.

list.toString() will print a list of the object in the list, enclosed in '[]' and seprated by ',' using the objects toString() method.

使用toString()方法通过使用system.out.println(list.toString() )打印列表的所有元素

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM