How to convert java.lang.Object to ArrayList?


I have a valid ArrayList object in the form of java.lang.Object. I have to again convert the Object to an ArrayList. I tried this:

Object obj2 = from some source . . ;
ArrayList al1 = new ArrayList();
al1 = (ArrayList) obj2;
System.out.println("List2 Value: "+al1);

But it is printing null. How can I do this?


Answers:


This only results in null if obj2 was already null before the cast, so your problem is earlier than you think. (Also, you need not construct a new ArrayList to initialize al1 if you're going to assign to it immediately. Just say ArrayList al1 = (ArrayList) obj2;.)