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++) {   

22 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.

Add toString() method to your address class then do


From what I understand you are trying to print an ArrayList of arrays and one way to display that would be


since you haven’t provide a custom implementation for toString() method it calls the default on which is going to print the address in memory for that object

solution in your Address class override the toString() method like this

public class Address {

int addressNo ; 

protected String toString(){
    return Integer.toString(addressNo);

now when you call

houseAddress.get(i)  in the `System.out.print()` method like this

System.out.print( houseAddress.get(i) ) the toString() of the Address object will be called

You can simply give it as:

System.out.println("Address:" +houseAddress);

Your output will look like [address1, address2, address3]

This is because the class ArrayList or its superclass would have a toString() function overridden.

Hope this helps.

assium that you have a numbers list like that

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6);

if you print the list

//method 1
// Conventional way of printing arraylist
for (int number : numbers) {

//method 2
// Lambda Expression to print arraylist
numbers.forEach((Integer value) -> System.out.print(value));

//method 3
// Lambda Expression to print arraylist
numbers.forEach(value -> System.out.print(value));

//method 4
// Lambda Expression (method reference) to print arraylist

Are you saying that ArrayList is storing addresses of arrays because that is what is returning from the toString call, or because that’s actually what you’re storing?

If you have an ArrayList of arrays (e.g.

int[] arr = {1, 2, 3};

Then to print the array values you need to call Arrays.deepToString:

for (int i = 0; i < houseAddress.size(); i++) {

public void printList(ArrayList<Address> list){
    for(Address elem : list){
        System.out.println(elem+"  ");

Since Java 8, you can use forEach() method from Iterable interface.
It’s a default method. As an argument, it takes an object of class, which implements functional interface Consumer. You can implement Consumer locally in three ways:

With annonymous class:

houseAddress.forEach(new Consumer<String>() {
    public void accept(String s) {

lambda expression:

houseAddress.forEach(s -> System.out.println(s));

or by using method reference:


This way of printing works for all implementations of Iterable interface.
All of them, gives you the way of defining how the elements will be printed, whereas toString() enforces printing list in one format.

Put houseAddress.get(i) inside the brackets and call .toString() function: i.e Please see below

for(int i = 0; i < houseAddress.size(); i++) {

I am not sure if I understood the notion of addresses (I am assuming houseAddress here), but if you are looking for way a to print the ArrayList, here you go:

System.out.println(houseAddress.toString().replaceAll("\[\]", ""));

 public static void main(String[] args) {
        List<Moyen> list = new ArrayList<Moyen>();
        Moyen m1 = new Moyen();
        Moyen m2 = new Moyen();
        Moyen m3 = new Moyen();


Simplest way to print an ArrayList is by using toString

List<String> a=new ArrayList<>();


[111, 112, 113]

You can use an Iterator. It is the most simple and least controvercial thing to do over here. Say houseAddress has values of data type String

Iterator<String> iterator = houseAddress.iterator();
while (iterator.hasNext()) {

Note : You can even use an enhanced for loop for this as mentioned by me in another answer

This helped to me:


if you make the @Override public String toString() as comments, you will have the same results as you did. But if you implement your toString() method, it will work.

public class PrintingComplexArrayList {

public static void main(String[] args) {
    List houseAddress = new ArrayList();

private static void insertAddress(List address)
    address.add(new Address(1));
    address.add(new Address(2));
    address.add(new Address(3));
    address.add(new Address(4));
private static void printMe1(List address)
    for (int i=0; i<address.size(); i++)

private static void printMe2(List address)


class Address{ private int addr; public Address(int i) { addr = i; }

@Override public String toString()
    Integer iAddr = new Integer (addr);
    return iAddr.toString();


You can even use an enhanced for loop or an iterator like:

for (String name : houseAddress) {

You can change it to whatever data type houseAddress is and it avoids unnecessary conversions


An alternative Solution could be converting your list in the JSON format and print the Json-String. The advantage is a well formatted and readable Object-String without a need of implementing the toString(). Additionaly it works for any other Object or Collection on the fly.

Example using Google’s Gson:


public static void printJsonString(Object o) {
    GsonBuilder gsonBuilder = new GsonBuilder();
     * Some options for GsonBuilder like setting dateformat or pretty printing
    Gson gson = gsonBuilder.create();
    String json= gson.toJson(o);

Make sure you have a getter in House address class and then use:

for(int i = 0; i < houseAddress.size(); i++) {   

you can use print format if you just want to print the element on the console.

for(int i = 0; i < houseAddress.size(); i++) {   
    System.out.printf("%s", houseAddress.get(i));

Assuming that houseAddress.get(i) is an ArrayList you can add toString() after the ArrayList :

for(int i = 0; i < houseAddress.size(); i++) {   

A general example:

ArrayList<Double> a = new ArrayList();
// output
// [2.0, 32.0]

This is a simple code of add the value in ArrayList and print the ArrayList Value

public class Samim {

public static void main(String args[]) {
    // Declare list
    List<String> list = new ArrayList<>();

    // Add value in list
    list.add("First Value ArrayPosition=0");
    list.add("Second Value ArrayPosition=1");
    list.add("Third Value ArrayPosition=2");
    list.add("Fourth Value ArrayPosition=3");
    list.add("Fifth Value ArrayPosition=4");
    list.add("Sixth Value ArrayPosition=5");
    list.add("Seventh Value ArrayPosition=6");

    String[] objects1 = list.toArray(new String[0]);

    // Print Position Value

    // Print All Value
    for (String val : objects1) {

Leave a Reply

Your email address will not be published. Required fields are marked *

We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.
Privacy Policy