ive created an array list but every time i try and print the conents, instead of giving me a list it gives me the class name followed by @e41bc3 random numbers and letter.
any ideas on how to print the contents?
cheers

Recommended Answers

All 2 Replies

Do it with and without toString() method

import java.util.ArrayList;
import java.util.List;

public class Main {

    public static void main(String[] args) {
        List<Any> list = new ArrayList<Any>();
        list.add(new Any(1));
        list.add(new Any(2));
        System.out.println(list);
    }
}

class Any {

    private final int i;

    public Any(int i) {
        this.i = i;
    }

    public String toString() {
        return "Any: " + i;
    }
}

You get this out put when you try to use th toArray function on the ArrayList instance not the toString :
Lets give ame examples:

    //It is supposed that the ArrayList holds the Integer type
    List<Integer> array =new ArrayList<Integer>();
    array.add(123);
    System.out.println(array.toArray());

The output will shows some thing like

[Ljava.lang.Object;@3e25a5

Where 3e25a5 is the momery adresss of the array object

But using this

   List<Integer> array =new ArrayList<Integer>();
   array.add(123);
   System.out.println(array.toString());

Will shows [123]

Hope it helps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.