Beny Bosko Beny Bosko - 2 years ago 510
Java Question

Java / Generics

I hava implemented a generic arraylist with

public Object[] toArray()
{
return elementData;
}


to be able to sort it later. When i try to get the elements out

ArrayList<Integer> list = new ArrayList<Integer>();

list.add(10000);
list.add(1000);
list.add(100);
list.add(10);
list.add(1);

Object[] a = list.toArray();

for(Object o:a)
{
System.out.println(a);
}


it prints "[Ljava.lang.Object;@2a139a55" and such things, however the runtime type must be Integer here, isn't it?

Answer Source

A typo here:

for(Object o:a)
{
    System.out.println(a);
}

should be

for(Object o:a)
{
    System.out.println(o);
}

By the way, just calling System.out.println(list); is enough here.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download