Join
Blogs
Questions
Videos
Tags
Members
Search
 
 
Create your own blog, earn points and get popular!

Java ArrayList toString

This Java example shows how to use Java ArrayList toString method to display its elements.
  1. import java.util.ArrayList;
  2.  
  3. //Java ArrayList toString
  4. public class JavaArrayListToString {
  5.    
  6.     public static void main(String args[]){
  7.        
  8.         ArrayList<Integer> aListNumbers = new ArrayList<Integer>();
  9.         aListNumbers.add(1);
  10.         aListNumbers.add(2);
  11.         aListNumbers.add(3);
  12.        
  13.         /*
  14.          * This will call toString method of Java ArrayList, which in turn
  15.          * calls toString method of Integer wrapper class.
  16.          *
  17.          * toString method of Integer wrapper class outputs underlying int
  18.          * value.
  19.          */
  20.         System.out.println(aListNumbers);
  21.  
  22.         //Create objects of custom Person class
  23.         Person p1 = new Person("Alex", 29);
  24.         Person p2 = new Person("Tanya", 21);
  25.        
  26.         //Create an ArrayList of objects Person
  27.         ArrayList<Person> aListPersons = new ArrayList<Person>();
  28.        
  29.         //add Objects of Persons to ArrayList
  30.         aListPersons.add(p1);
  31.         aListPersons.add(p2);
  32.        
  33.         /*
  34.          * This will call toString method of ArrayList class and in turn
  35.          * it will call toString method of underlying class, that is in this
  36.          * case Person class.
  37.          *
  38.          * If the Person class does not have toString defined, toString method
  39.          * of Object class is called which prints default output similar to
  40.          * as shown below.
  41.          *
  42.          * [Person@1c161c16, Person@1c1c1c1c]
  43.          *
  44.          * This kind of output is not useful, as we expect it to print
  45.          * name and age of person added to ArrayList. In this case we can
  46.          * override toString method of Person class.
  47.          *
  48.          */
  49.         System.out.println(aListPersons);
  50.        
  51.     }
  52. }
  53.  
  54. class Person{
  55.    
  56.     String name;
  57.     int age;
  58.    
  59.     public Person(String name, int age){
  60.         this.name = name;
  61.         this.age = age;
  62.     }
  63.    
  64.     public String getName(){
  65.         return this.name;
  66.     }
  67.    
  68.     public int getAge(){
  69.         return this.age;
  70.     }
  71.    
  72.     public String toString(){
  73.         return this.getName() + "-" + this.getAge();
  74.     }
  75.    
  76. }
  77.  
  78. /*
  79. Output of above given Java ArrayList toString method would be
  80. [Alex-29, Tanya-21]
  81. [1, 2, 3]
  82. */
Your rating: None Average: 5 (2 votes)
Share this
Anonymous's picture

WTF ,this is a manual

WTF ,this is a manual toString method ... there is an in-built java tostring in arraylist.

Post new comment

5 + 0 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.