Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print ArrayList

I have an ArrayList that contains Address objects.

How do I print the values of this ArrayList, meaning I am printing out the contents of the Array, in this case numbers.

I can only get it to print out the actual memory address of the array with this code:

for(int i = 0; i < houseAddress.size(); i++) {        System.out.print(houseAddress.get(i)); }   
like image 970
dancooper93 Avatar asked Feb 13 '12 18:02

dancooper93


People also ask

How do you print ArrayList?

These are the top three ways to print an ArrayList in Java: Using a for loop. Using a println command. Using the toString() implementation.

Can we directly print ArrayList in Java?

Print ArrayList in java using toString() If you just want to print ArrayList on console, you can directly print it using its reference. It will call toString() method internally of type of ArrayList( String in this example).


1 Answers

list.toString() is good enough.

The interface List does not define a contract for toString(), but the AbstractCollection base class provides a useful implementation that ArrayList inherits.

like image 98
oldo Avatar answered Sep 20 '22 15:09

oldo