As we all know in Java if you change a object 'everyone' that has a reference to that object also changes like in the following example.
List<String> mainList = new ArrayList<String>();
mainList.add("a");
mainList.add("c");
mainList.add("b");
mainList.add("d");
System.out.println(Arrays.toString(mainList.toArray(new String[mainList.size()]))); // randomly ordered strings [a, c, b, d]
List<String> referencedList = mainList;// referencedList contains the same unordered strings [a, c, b, d]
Collections.sort(mainList);// mainList is sorted [a, b, c, d]
System.out.println(Arrays.toString(referencedList.toArray(new String[ referencedList.size()]))); // the referenced list is also sorted
Is there any way to get a List<> that only the objects within are updated across all of the arrays but the referenced arrays stay unsorted?
It sounds like you just want a shallow clone, e.g.
List<String> referencedStrings = new ArrayList<>(referencedStrings);
That will take a copy of the existing list - but as the list only contains references, any changes to the objects those references refer to will be visible via both lists. (Not that you could change the contents of String
objects anyway, mind you...)
See more on this question at Stackoverflow