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
Post a Comment