-1

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?

5
  • 3
    new String[]{"ead","sda","eps"}; //change my array but non change my list No. That throws away your old array and makes a new array. Commented Dec 2, 2022 at 17:01
  • it dosn't throw away my array, it initialize new array with new object, i think, but old array remains related to same list. aren't? @ElliottFrisch
    – Lukas
    Commented Dec 2, 2022 at 17:07
  • Trying to put this into simple words: There is a difference between variables and the Objects those variables point to. At the beginning you have a variable nameListLinkedToArrayFixedSize that points to an Array Object. You then create an ArrayList and that List internally points to the same Array Object. The List doesn't point to your variable, but the Array Object itself. Then you create another Array Object and have your variable nameListLinkedToArrayFixedSize point to that new Object.
    – user6073886
    Commented Dec 2, 2022 at 17:08
  • 1
    Before any discussion you need to familiarize yourself with What is the difference between a variable, object, and reference?.
    – Pshemo
    Commented Dec 2, 2022 at 17:08
  • 2
    ok so i understood what's going around well, i have new array without relationship to my list.
    – Lukas
    Commented Dec 2, 2022 at 17:20

2 Answers 2

0

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]
0
-1

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

1
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Dec 6, 2022 at 8:38

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.