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 :)

do in this way:

...............
Object x;
x = s.Pop();
ArrayList al=x as ArrayList ;
if(al!=null)
{
 al.Add("Something");
}
...............

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.