A: 

The array classes all expose public clone() methods, however, so if a shallow copy of the array is sufficient then

return (ElementType[]) myArray.clone();

Makes for a nice, simple, easy to read paradigm. If "ElementType" is a primitive type then there is no distinction between shallow and deep copying, and if it is immutable then performing a deep copy would be wasteful. In some other circumstances a shallow copy is what you would want anyway (you want the receiver to be able to mutate the returned array elements with effects visible to the host object).

If you find that you really do want to make a deep copy of the array then you might want to consider whether your design can be improved; it is not necessarily so, but I wouldn't be surprised.

ayush
+1  A: 

I think your question is unclear, what do you mean by

"And what I'm doing is creating a new list with updated values. But I want to put those new values into the original ArrayList. This is important because I'm passing the original arraylist reference to an object that I no longer have access to, but I need to keep its contents up to date. Is there any way to make java copy the contents into a current reference? I hope that makes sense."

When you do

List a = new ArrayList
List b = a

you have one ArrayList object, and two references to the object, a and b.

Also note that there is an addAll method that you can use to add members of one collection to another collection. Note that I believe addAll does not do a deep copy, so if you use it both lists have copies of the same reference. So if list a has references a1, a2, a3, and you do b.addAll(a), b now has references to the objects that a1...a3 point to...

hvgotcodes
I ended up going with the addAll method to maintain a reference to the list
Falmarri
@Falmarri, just remember that when you add all, you are adding copies of the references, so the references in both the arrays point to the same underlying objects.
hvgotcodes
Yeah, I don't actually do anything with the underlying objects them selves. I'm just removing, adding, and rearranging them in the list.
Falmarri
A: 

In Java as I hope you;ve found out by now, all variables are references. Among other things this means that unless they are assigned to an object they don't 'point' at anything. You need to write:

ArrayList a = new ArrayList();

or a doesn't 'point to' an actual object - it's just null.

If you write:

ArrayList a = new ArrayList();
ArrayList b = a;
modify(b);
ArrayList c = b;

then there is only one ArrayList. All the variables a, b and c refer to it. Any modifications done in the 'modify' method apply to it, and will be reflect in the state of a, b and c.

DJClayworth
A: 

You're C++ code says this:

ArrayList a;            //make arraylist
ArrayList *b = a;       //set b to point to same arraylist as a
//pass b somewhere
ArrayList c;            //make arraylist
*b = c;                 //variable b in THIS PROGRAM now points to c. arraylist is unchanged.

You want to update the arraylist, not the pointer to it, as that pointer only 'exists' in the current context. The code you passed the arraylist to doesn't give a darn if a pointer back in who-knows-where now points to the same arraylist that its using. It's the actual object the other code cares about. To update that object, you can just call methods on it, like a.add(bar)

BUT there's more to it. If you call a function you don't control (now known as foo) and pass it an arraylist, that's all fine. But if you want to update it from the code calling foo, you run into issues. Imagine if an object you were working with could change randomly at any time. All sorts of bad stuff could happen. If you really need this capability (and we can help you judge if you do), look into the topic of threading.

CrazyJugglerDrummer
I understand your concern, but this is actually what the object that I'm passing my a to expects to happen. Also, I get your concerns about context, but I just threw that together really quick to get my point across. I left out actually calling new, and scoping, and all that.
Falmarri