tags:

views:

90

answers:

4

how do i print the element "e" in arraylist "list" out?

 ArrayList<Dog> list = new ArrayList<Dog>();
 Dog e = new Dog();
 list.add(e);
 System.out.println(list);
+1  A: 

Your code requires that the Dog class has overridden the toString() method so that it knows how to print itself out. Otherwise, your code looks correct.

Topher Fangio
A: 

First make sure that Dog class implements the method public String toString() then use

System.out.println(list.get(index))

where index is the position inside the list. Of course since you provide your implementation you can decide how dog prints itself.

Jack
I really would like to know why people downvote withoutany explaination..
Jack
I agree it would be reasonable to accept downvotes only together with a word of explanation.
mloskot
+3  A: 
sateesh
A: 

You should override toString() method in your Dog class. which will be called when you use this object in sysout.

GK