Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Created two Lists from same array, Modifying one List, changes the other

I created two list from same array and sorted one of them. When I tried to change one list, other list also got updated.

List<Integer> list = Arrays.asList(ar);
List<Integer> sorted = Arrays.asList(ar);
Collections.sort(sorted);
list.set(0,10000000); //changes sorted also

It took me a while figure out, below mentioned code worked.

List<Integer> sorted = new ArrayList<Integer>(Arrays.asList(ar));

I want to know why my first approach didn't work? I created two separate lists, why the changes are taking place in both of them. How does java assign values to variables here?

like image 558
Charan Avatar asked Aug 19 '15 19:08

Charan


People also ask

How do I change my new list without changing the original list Python?

Howerver, there is one problem with copying lists in this way. If you modify new_list , old_list is also modified. It is because the new list is referencing or pointing to the same old_list object. However, if you need the original list unchanged when the new list is modified, you can use the copy() method.

Are nested lists mutable?

Lists can contain any arbitrary objects. List elements can be accessed by index. Lists can be nested to arbitrary depth. Lists are mutable.

How do I make two lists the same in Python?

We can club the Python sort() method with the == operator to compare two lists. Python sort() method is used to sort the input lists with a purpose that if the two input lists are equal, then the elements would reside at the same index positions.


2 Answers

From the Java documentation for Arrays.asList:

Returns a fixed-size list backed by the specified array. (Changes to the returned list "write through" to the array.) This method acts as bridge between array-based and collection-based APIs, in combination with Collection.toArray(). The returned list is serializable and implements RandomAccess.

So when you change something in list, it "writes through" to the underlying array, ar, which is also the underlying array in sorted, so the change is reflected in sorted as well.

Also, the code for asList is:

public static <T> List<T> asList(T... a) {
    return new ArrayList<T>(a);
}

This is java.util.Arrays.ArrayList, which has the following definition:

ArrayList(E[] array) {
    a = Objects.requireNonNull(array);
}

What is important is that a is not copied, it is the original array. The java.util.ArrayList class has the following constructor

public ArrayList(Collection<? extends E> c) {
     elementData = c.toArray();
     size = elementData.length;
     // c.toArray might (incorrectly) not return Object[] (see 6260652)
     if (elementData.getClass() != Object[].class)
         elementData = Arrays.copyOf(elementData, size, Object[].class);
 }

so in the java.util.ArrayList constructor, we create copies of each element, and in java.util.Arrays.ArrayList, we do not.

like image 161
Sunde Avatar answered Sep 30 '22 12:09

Sunde


Arrays has its own implementations of ArrayList which does not make a copy of array from toList

like image 43
krzydyn Avatar answered Sep 30 '22 12:09

krzydyn