<p>I have an <code>ArrayList&lt;String&gt;</code> that I'd like to return a copy of.  <code>ArrayList</code> has a clone method which has the following signature:</p>

<pre><code>public Object clone()
</code></pre>

<p>After I call this method, how do I cast the returned Object back to <code>ArrayList&lt;String&gt;</code>?</p>

[{'answer_id': 54911, 'author': 'jodonnell', 'author_id': 4223, 'author_profile': 'https://Stackoverflow.com/users/4223', 'pm_score': 1, 'selected': False, 'text': '<pre><code>ArrayList first = new ArrayList ();\nArrayList copy = (ArrayList) first.clone ();\n</code></pre>\n'}, {'answer_id': 54912, 'author': 'Vinko Vrsalovic', 'author_id': 5190, 'author_profile': 'https://Stackoverflow.com/users/5190', 'pm_score': 7, 'selected': True, 'text': '<pre><code>ArrayList newArrayList = (ArrayList) oldArrayList.clone();\n</code></pre>\n'}, {'answer_id': 54917, 'author': 'Allain Lalonde', 'author_id': 2443, 'author_profile': 'https://Stackoverflow.com/users/2443', 'pm_score': 4, 'selected': False, 'text': '<p>I find using addAll works fine.</p>\n\n<pre><code>ArrayList&lt;String&gt; copy = new ArrayList&lt;String&gt;();\ncopy.addAll(original);\n</code></pre>\n\n<p>parentheses are used rather than the generics syntax</p>\n'}, {'answer_id': 54922, 'author': 'pkaeding', 'author_id': 4257, 'author_profile': 'https://Stackoverflow.com/users/4257', 'pm_score': 2, 'selected': False, 'text': '<p>This should also work:</p>\n\n<pre><code>ArrayList&lt;String&gt; orig = new ArrayList&lt;String&gt;();\nArrayList&lt;String&gt; copy = (ArrayList&lt;String&gt;) orig.clone()\n</code></pre>\n'}, {'answer_id': 55132, 'author': 'Aaron', 'author_id': 2628, 'author_profile': 'https://Stackoverflow.com/users/2628', 'pm_score': 4, 'selected': False, 'text': '<p>I think this should do the trick using the Collections API:</p>\n\n<p><strong>Note</strong>: the copy method runs in linear time.    </p>\n\n<pre><code>//assume oldList exists and has data in it.\nList&lt;String&gt; newList = new ArrayList&lt;String&gt;();\nCollections.copy(newList, oldList);\n</code></pre>\n'}, {'answer_id': 56383, 'author': 'Tom Hawtin - tackline', 'author_id': 4725, 'author_profile': 'https://Stackoverflow.com/users/4725', 'pm_score': 8, 'selected': False, 'text': '<p>Why would you want to clone? Creating a new list usually makes more sense.</p>\n\n<pre><code>List&lt;String&gt; strs;\n...\nList&lt;String&gt; newStrs = new ArrayList&lt;&gt;(strs);\n</code></pre>\n\n<p>Job done.</p>\n'}, {'answer_id': 350301, 'author': 'Julien Chastang', 'author_id': 32174, 'author_profile': 'https://Stackoverflow.com/users/32174', 'pm_score': 4, 'selected': False, 'text': '<p>Be advised that Object.clone() has some major problems, and its use is discouraged in most cases. Please see Item 11, from "<a href="http://java.sun.com/docs/books/effective/" rel="noreferrer">Effective Java</a>" by Joshua Bloch for a complete answer. I believe you can safely use Object.clone() on primitive type arrays, but apart from that you need to be judicious about properly using and overriding clone. You are probably better off defining a copy constructor or a static factory method that explicitly clones the object according to your semantics.</p>\n'}, {'answer_id': 599155, 'author': 'Juan Besa', 'author_id': 72369, 'author_profile': 'https://Stackoverflow.com/users/72369', 'pm_score': 2, 'selected': False, 'text': '<p>Be very careful when cloning ArrayLists. Cloning in java is shallow. This means that it will  only clone the Arraylist itself and not its members. So if you have an ArrayList X1 and clone it into X2 any change in X2 will also manifest in X1 and vice-versa. When you clone you will only generate a new ArrayList with pointers to the same elements in the original. </p>\n'}, {'answer_id': 8971483, 'author': 'Petr', 'author_id': 1164753, 'author_profile': 'https://Stackoverflow.com/users/1164753', 'pm_score': 1, 'selected': False, 'text': '<p>I am not a java professional, but I have the same problem and I tried to solve by this method. (It suppose that T has a copy constructor).</p>\n\n<pre><code> public static &lt;T extends Object&gt; List&lt;T&gt; clone(List&lt;T&gt; list) {\n      try {\n           List&lt;T&gt; c = list.getClass().newInstance();\n           for(T t: list) {\n             T copy = (T) t.getClass().getDeclaredConstructor(t.getclass()).newInstance(t);\n             c.add(copy);\n           }\n           return c;\n      } catch(Exception e) {\n           throw new RuntimeException("List cloning unsupported",e);\n      }\n}\n</code></pre>\n'}, {'answer_id': 10251692, 'author': 'GeRmAn', 'author_id': 1347278, 'author_profile': 'https://Stackoverflow.com/users/1347278', 'pm_score': 4, 'selected': False, 'text': '<p>This is the code I use for that:</p>\n\n<pre><code>ArrayList copy = new ArrayList (original.size());\nCollections.copy(copy, original);\n</code></pre>\n\n<p>Hope is usefull for you</p>\n'}, {'answer_id': 25785306, 'author': 'Ahmed Hamdy', 'author_id': 319876, 'author_profile': 'https://Stackoverflow.com/users/319876', 'pm_score': 2, 'selected': False, 'text': '<p>To clone a generic interface like <code>java.util.List</code> you will just need to cast it. here you are an example:</p>\n\n<pre><code>List list = new ArrayList();\nList list2 = ((List) ( (ArrayList) list).clone());\n</code></pre>\n\n<p>It is a bit tricky, but it works, if you are limited to return a <code>List</code> interface, so anyone after you can implement your list whenever he wants.</p>\n\n<p>I know this answer is close to the final answer, but my answer answers how to do all of that while you are working with <code>List</code> -the generic parent- not <code>ArrayList</code></p>\n'}, {'answer_id': 35014446, 'author': 'Simon Jenkins', 'author_id': 5763141, 'author_profile': 'https://Stackoverflow.com/users/5763141', 'pm_score': 4, 'selected': False, 'text': '<p>With Java 8 it can be cloned with a stream.</p>\n\n<pre><code>import static java.util.stream.Collectors.toList;\n</code></pre>\n\n<p>...</p>\n\n<pre><code>List&lt;AnObject&gt; clone = myList.stream().collect(toList());\n</code></pre>\n'}, {'answer_id': 35430941, 'author': 'Uri Shalit', 'author_id': 5019818, 'author_profile': 'https://Stackoverflow.com/users/5019818', 'pm_score': 3, 'selected': False, 'text': '<p>If you want this in order to be able to return the List in a getter it would be better to do:</p>\n\n<pre><code>ImmutableList.copyOf(list);\n</code></pre>\n'}, {'answer_id': 54040913, 'author': 'Robert', 'author_id': 6113110, 'author_profile': 'https://Stackoverflow.com/users/6113110', 'pm_score': 3, 'selected': False, 'text': "<pre><code>List&lt;String&gt; shallowClonedList = new ArrayList&lt;&gt;(listOfStrings);\n</code></pre>\n\n<p>Keep in mind that this is only a shallow not a deep copy, ie. you get a new list, but the entries are the same. This is no problem for simply strings. Get's more tricky when the list entries are objects themself.</p>\n"}]
['https://Stackoverflow.com/questions/54909', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1288/']