My, perhaps naive, solution for cloning an ArrayList (Vector replacement) is
ArrayList<Double> alBis = (ArrayList<Double>) alOriginal.clone();
considering that because the array contains immutable Doubles, I don’t need
to clone them, but only the container.
As clone() returns an Object I put there the cast, but then
-Xlint complains it is an unchecked cast.
So, what now? Ignore it with supressWarnings? Create a new ArrayList
and copy the orginal elements with a compact for? Any library method similar to
Arrays.copyOf()?
I read Unchecked cast warning but the accepted way is incredible complex.
clone()has major flaws, see this Question for reference. Don’t use it!Instead, all standard Collections have copy constructors. Use them:
Reference:
ArrayList<E>(Collection)constructor