java - How to clone a container (f.e. ArrayList) *easily*? Is it wrong to use .clone()? -


my, perhaps naive, solution cloning arraylist (vector replacement) is

arraylist<double> albis = (arraylist<double>) aloriginal.clone(); 

considering because array contains immutable doubles, don't need clone them, container.

as clone() returns object put there cast, -xlint complains unchecked cast.

so, now? ignore supresswarnings? create new arraylist , copy orginal elements compact for? library method similar arrays.copyof()?

i read unchecked cast warning accepted way incredible complex.

clone() has major flaws, see this question reference. don't use it!

instead, standard collections have copy constructors. use them:

list<double> original = // list list<double> copy = new arraylist<double>(original); 

reference:


Comments

Popular posts from this blog

Javascript line number mapping -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -