Tuesday, 17 September 2013

Storing an Object into a temporary Object and not changing the original Objects value

Storing an Object into a temporary Object and not changing the original
Objects value

I have an object stored in a global variable let's say:
static ArrayList<Object> list = new ArrayList<Object>();
I want to store it later to look into it without actually changing the
values in the structure itself. So I am doing something similar to this:
public void someMethod()
{
ArrayList<Object> tempList = new ArrayList<Object>();
tempList = list;
list.remove(0);
}
I'm thinking this may have something to do with me initializing the
variable as "static". I don't usually do that but Eclipse told me I had to
so I just let the change happen.
My understanding would be that I am storing the original list into a
temporary list and anything I do to the temporary list would be
independent of the original list. But it appears that if I were to remove
something from this above list, that the original list is removing it as
well.
I remember learning that this could happen sometimes but I think I've done
this before without having that issue.
I apologize if this is a repeated question but the way I worded it didn't
show me an question that was similar.
Thanks!

No comments:

Post a Comment