Home > Blockchain >  Java - from Array to linked ArrayList why it doesn't affect all int values?
Java - from Array to linked ArrayList why it doesn't affect all int values?

Time:12-03

I am studing Java, i have simple Array linked to ArrayList, it is fixed size i can change values inside array or list without change length. So i tried to change all elements of the Array to see changes into ArrayList (it doesn't work). I saw that if i change single value into Array my list would change too (it works). If i change my List values into array wuold changed. If i change List or Array length would throw exception.

 String[] nameListLinkedToArrayFixedSize = {"Jhonny","Joe","Jhoseph"};
 List<String> nameListLinkedToArray = Arrays.asList(nameListLinkedToArrayFixedSize);
 nameListLinkedToArrayFixedSize[1] = "J.Joe"; // this change my list
 nameListLinkedToArrayFixedSize = new String[]{"ead","sda","eps"}; //change my array but non change my list
 System.out.println(nameListLinkedToArray)  // is same as first array why?
 nameListLinkedToArray.set(2, "J.Jhoseph"); //[Jhonny, J.Joe, J.Jhoseph]

I need to understand how works linked arrays, i suppose this is not go well without point new array to new linked list?
Why single operation on array change list? What is pointer of linked list after i change all element of array? Why my list continues update old values of array? Where to find specific documentation?

CodePudding user response:

You have to dig into the code for Arrays.asList(). When you do, you will see that it uses the array you pass to it as the backing store. If you make changes to items in that array going forward in your code, those changes will also be seen in elements of the list.

However, if you assign your original array to a new array, you have effectively de-linked your external reference to the backing store, hence changes to elements of the new array are not reflected in the list.

import java.util.Arrays;
import java.util.List;

public class ArraysAsListSideEffects {

    public static void main(String[] args) {

        codeWithSideEffects();

        codeWithoutSideEffects();
    }

    private static void codeWithoutSideEffects() {
        System.out.println("\n\nCode without side effects: ");

        String[] originalArray = { "Jhonny", "Joe", "Jhoseph" };
        List<String> listFromArray = List.of(originalArray);
        System.out.println(listFromArray);

        originalArray[1] = "J.Joe";
        System.out.println("After updating original array: "   listFromArray);
    }

    protected static void codeWithSideEffects() {
        System.out.println("Side effects of using Arrays.asList()");
        String[] originalArray = { "Jhonny", "Joe", "Jhoseph" };
        List<String> listFromArray = Arrays.asList(originalArray);

        // Because Arrays.asList uses your original array as the
        // backing store, changing an element in the original
        // array changes the element in the list.
        originalArray[1] = "J.Joe"; // this change my list

        // change my array but non change my list
        originalArray = new String[] { "ead", "sda", "eps" };
        // Since you assigned original array to a new array,
        // changes to it will not affect the backing store reference
        // used by listFromArray.

        System.out.println(listFromArray); // is same as first array why?
                                           // Yes because Arrays.asList stores a reference to that
                                           // list.

        listFromArray.set(2, "J.Jhoseph"); // [Jhonny, J.Joe, J.Jhoseph]} 
                                            // This is correct because of how Arrays.asList
                                            // creates an ArrayList with your original array
                                            // as backing store.
        System.out.println(listFromArray);
    }
}

Output:

Side effects of using Arrays.asList()
[Jhonny, J.Joe, Jhoseph]
[Jhonny, J.Joe, J.Jhoseph]


Code without side effects: 
[Jhonny, Joe, Jhoseph]
After updating original array: [Jhonny, Joe, Jhoseph]

CodePudding user response:

I think you understand that the list array value is defined upper but when you change the string[] it doesn't affect le list so you have to change it again

  • Related