-2

if I ge

ArrayList<String> myList;
// add some stuff to myList
ArrayList<String> copyOf=myList;

copy will be a reference to myList, thus if I change copyOf, myList will change to. Hpw can I make coyyOf be a copy of myList, so if copyOf chnages, myList will not change.

shmosel
  • 49,289
  • 6
  • 73
  • 138
Ted pottel
  • 6,869
  • 21
  • 75
  • 134

1 Answers1

1

Use the constructor of ArrayList that takes a collection as a parameter.

List<String> myCopy = new ArrayList<>(theArrayListToCopy);

According to the Javadoc, this "Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator."

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110