-4

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?

dur
  • 15,689
  • 25
  • 79
  • 125
Beny Bosko
  • 239
  • 1
  • 7

1 Answers1

4

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.

Dmitry P.
  • 824
  • 5
  • 14
  • Or, if this is not an example, and you really just want to print the array: http://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-a-java-array – Thilo Feb 06 '16 at 12:05
  • 1
    Btw, you can iterate over the list itself, no need to transform it into an array. Lists also have a "usable" `toString` method. `for(Object o: list){` – Thilo Feb 06 '16 at 12:07
  • @Thilo what is wrong with my answer in the comments? – Faraz Feb 06 '16 at 12:08