How to clone a container (e.g. ArrayList) * easy *? Is it wrong to use .clone ()?

My possibly naive solution for cloning an ArrayList (vector replacement)

ArrayList<Double> alBis = (ArrayList<Double>) alOriginal.clone();

given that since the array contains immutable pairs, I don't need to clone them, but only a container.

Since clone () returns the object, I put a throw there, but then -Xlint complains that this is an uncontrolled selection.

So what now? Ignore it with supressWarnings? Create a new ArrayList and copy the original elements with a compact? Any library method similar to Arrays.copyOf ()?

I am reading an Immediate Warning Warning , but the accepted method is incredibly complicated.

+3
source share
3

clone() , . . !

. :

List<Double> original = // some list
List<Double> copy = new ArrayList<Double>(original);

:

+13

.clone()?

, . , API. , @Sean.

+4

Google guava-libraries :

 ImmutableCollection<Double> copy = ImmutableList.copyOf(original);

JRE-, ( , , ).

:

+1

Source: https://habr.com/ru/post/1794214/


All Articles