Change nodes ArrayList

5

If I have a ArrayList<Integer> listInt for example, suppose that ArrayList has:

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

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

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

    
asked by anonymous 08.02.2014 / 06:48

2 answers

6

No, because a , b and c are of type int , which is a primitive type . In Java, anything 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 ArrayList we have an additional complication: it only accepts objects as list elements, not primitive types. In older Java versions, it was even mandatory to explicitly create a Integer from your int s before moving to ArrayList :

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

From a certain version (I can not quite remember) Java has implemented the autoboxing feature - which allows you to use int and Integer as if (although "underneath the cloths" it continues to create the object for you, so this 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 out of the list affects the objects within the list is if the added objects are changeables , and you are tinkering with the content of them - not in your 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)

For autoboxing , however, there would be no problem even if a , b and c were Integer : since it is immutable . Since you can not modify it once it is created, nothing you do outside of ArrayList will affect the values inside it.

    
08.02.2014 / 07:24
2

When you do:

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 type Integer before adding it to your ArrayList and initializing those objects with values 2, 3, and 4. This is done automatically with a mechanism called Autoboxing , it creates an object without the need for the new operator to automatically convert a primitive to an object on occasions that are reserved for objects, such as ArrayList .

Assuming you did:

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

You may think that you can now change the value of Integers objects that are within ArrayList , but this does not happen because on lines 9, 10 and 11 you again through the Autoboxing and Autounboxing is creating new objects. This happens because Integers are immutable, that is, you can not change the value of the object, but you can create a new object with a new value and make its reference variables a , b and c refer to this new object.

    
08.02.2014 / 18:52