0

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

3
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by moutanna
0

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;
    }
}
0

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.

Edited by mike_2000_17: Fixed formatting

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.