954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Unboxing an ArrayList in an object

I'm probably going about this in the wrong way, but I have a stack of Objects, and some of these can be ArrayLists. I want to be able to pop an object that is actually an ArrayList off the stack, unbox it, add something, and then pop it back on. For example:

Stack<Object> s = new Stack<Object>();
ArrayList l = new ArrayList();
Object x;
s.Push(l);
x = s.Pop();
x = (ArrayList)x; //This is the line I don't know how to do
x.Add("Something");
s.push(x);


Does anyone have an idea of how to do what I'm trying to do?

Thanks in advance :)

tdeck
Light Poster
31 posts since Jul 2009
Reputation Points: 17
Solved Threads: 0
 

do in this way:

...............
Object x;
x = s.Pop();
ArrayList al=x as ArrayList ;
if(al!=null)
{
 al.Add("Something");
}
...............
DangerDev
Posting Pro in Training
485 posts since Jan 2008
Reputation Points: 165
Solved Threads: 59
 

Thanks, that works the way it should! Apparently what made the difference was the use of a new name for the created arrayList.

tdeck
Light Poster
31 posts since Jul 2009
Reputation Points: 17
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You