-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
  • 45,768
  • 6
  • 62
  • 130
Ted pottel
  • 6,721
  • 19
  • 72
  • 130

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
  • 73,541
  • 13
  • 95
  • 104