Change Arraylist nodes

Asked

Viewed 1,144 times

5

If I have a ArrayList<Integer> listInt for example, suppose in this ArrayList have:

int a, int b, int c; //a = 2 b=3 c=4

and then I change the value of a, b and c for example to a=1 b=2 c=3

Because I have worked with the same "objects" stored in the arraylist, when I access it, I get the new data?

2 answers

6


No, because a, b and c are of the type int, which is a primitive type. In Java, everything that is passed as an argument to a function (including constructors) is passed by value (i.e. copied). Even a reference is passed by value (i.e. the object may not be copied, but the "pointer" to it is).

int a = 10;
foo(a); // "foo" recebe uma cópia de a

In the case of a ArrayList We have an additional complication: he only accepts objects as list elements, not primitive types. In older versions of Java, it was even mandatory that you explicitly create a Integer from their ints before passing to ArrayList:

int a = 10;
meuArrayList.add(new Integer(a));

From a certain version (I don’t quite remember which) Java implemented the functionality autoboxing - that allows you to use int and Integer as if it were only one thing (although "underneath the scenes" it keeps creating the object for you, so that it affects performance and memory usage):

int a = 10;
meuArrayList.add(a); // Na verdade, está sendo adicionado: new Integer(a)
int b = meuArrayList.get(0); // Na verdade, está sendo retornado:
                             // meuArrayList.get(0).intValue()

The only situation where a modification made outside the list affects the objects within the list is if the added objects are mutable, and you’re messing with the contents of them - not in their reference:

int[] a = new int[]{ 10, 20, 30 };
meuArrayList.add(a);

// Mexeu no conteúdo de a
a[1] = 40;
meuArrayList.get(0)[1]; // 40

// Mexeu na referência a
a = new int[]{ 50, 60, 70 };
meuArrayList.get(0)[1]; // 40 (continua sendo o array antigo)

In the case of autoboxing, however, there would be no problem even if a, b and c were the type Integer: since he is immutable. As we can not modify it once created, nothing you do outside the ArrayList will affect the values within it.

2

When Voce does:

List<Integer> listInt = new ArrayList<>();

int a = 2, b = 3, c = 4;

listInt.add(a); //linha 5
listInt.add(b); //linha 6
listInt.add(c); //linha 7

On lines 5, 6 and 7 you are creating 3 objects of the type Integer before adding it to your ArrayList and is initiating these objects with the values 2, 3 and 4. This is done automatically with a mechanism called Autoboxing, it creates an object without the need of the operator new to automatically convert a primitive to an object on occasions that are reserved for objects, as is the case with Arraylist.

Supposing you had done:

List<Integer> listInt = new ArrayList<>();

Integer a = 2, b = 3, c = 4;

listInt.add(a);
listInt.add(b);
listInt.add(c);

a *= 2; //linha 9
b *= 2; //linha 10
c *= 2; //lina 11

Maybe you think you can change the value of objects now Integers who are inside the ArrayList, but this does not happen because in lines 9, 10 and 11 you again through the mechanisms of Autoboxing and Autounboxing is creating new objects. This happens because Integers are immutable, ie you can not change the value of the object, but you can create a new object with a new value and make your reference variables a, b and c refer to this new object.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.