Why don’t you instantiate the list so it doesn’t accumulate?

Asked

Viewed 108 times

1

I have the following class:

public class TesteLista {

 public static void main(String[] args) {
    List<String> dados;

    Teste1 t = new Teste1();
    dados = t.getList();

    System.out.println(dados);
 }
}

Class that has the method:

public class Teste1 {

public List<String> getList(){
    List<String> lista = new ArrayList<>();
    lista.add("Aline");
    lista.add("João");
    lista.add("Jona");
    lista.add("Pedro");

        return lista;
 }
}

He prints:

[Aline, João, Jona, Pedro]

Why does the data list not accumulate since I do not give new? Why don’t I need to give dados= new ArrayList<>(); not to accumulate?

  • Why do you have a main() inside Teste1? Set "accumulate". Can’t new?

  • It’s like the start of my code.

  • It doesn’t seem to make sense. Which of the two is the main() in fact? Alias, what is the doubt?

  • My question is why I do not need to give new in the data list? And why it does not accumulate???

  • accumulate is like doing this with the given list: [Aline, John, Jona, Peter Aline, John, Jona, Peter, Aline, John, Jona, Peter] to each run more on the list

  • 3

    This you’ve already written, now explain what this means.

Show 1 more comment

2 answers

6


What you’re doing is a copy of references of a kind List among variables. That is why you do not need to instantiate, because you are already assigning an element that has already been instantiated and copying its reference to the variable dados.

Imagine your code was like this:

List<String> dados;

...

List<String> lista = new ArrayList<>();
lista.add("Aline");
lista.add("João");
lista.add("Jona");
lista.add("Pedro");

...

dados = lista;

This is more or less what is happening. The variable dados is nula when started, but when assigning it the reference of the created list within its class Teste01, she goes on to "point" to that list.

  • 1

    Awwwwwwwwwwwwwwwww got it :O

4

I think your interpretation of what the code is is wrong. You’re probably not thinking about how he’s executed, I imagine you think you put words in and magically something happens. Everything that is written has a consequence and it is necessary to understand the whole process, otherwise it will not learn to program.

Let’s understand what happens with the list:

On the line

List<String> lista = new ArrayList<>();

a list of strings and is referenced by a variable called lista.

On the following lines

lista.add("Aline");
lista.add("João");
lista.add("Jona");
lista.add("Pedro");

elements are added to this list.

Finally on the line

return lista;

the list is returned to the caller. The variable lista ceases to exist, but the object pointed out by it, in case the list with 4 elements still exists and will be pointed out by the one who called this method.

We now return to the main method. On the line

Teste1 t = new Teste1();

an object of the type Teste1 and placed in the variable `t . Actually, there wouldn’t have to be an object just for this, but come on.

On the line

dados = t.getList();

the method is called and its return, that our list is passed to the variable data that had already been declared. So now dados now has the list that was in the variable lista within the method getList().

Finally the line

System.out.println(dados);

prints the list.

I put in the Github for future reference.

You don’t need another new anywhere because you only need to create the list once. You have nothing to "accumulate" because the code does not command you to do this. If he had it done then he would. It would be impossible to have [Aline, João, Jona, Pedro Aline, João, Jona, Pedro, Aline, João, Jona, Pedro] because only a list with only 4 elements was created.

You have to understand what the code is doing or you keep repeating code ready and you don’t know what you’re doing.

I imagine you want to do something else, but the question doesn’t even mention it. Before programming, it is necessary to understand the problem and express it in Portuguese. If this cannot be done, it is certain that in Java it will not be right, since the base is wrong.

  • Hummmmmm. I found your answer arrogant. I did not understand, it was I who created all this little codegozinho and you tell me that I copied from somewhere else?

  • But I understood here I only have to create four items...Thanks anyway...

  • 4

    I said you don’t understand what you’re doing. You’ve been on the site a long time and still don’t understand how the codes work. I understand that to hear that we don’t like it sounds arrogant. Some people take it like that and don’t evolve, others take the hint and start taking a step forward. Evolution is a matter of the attitude of recognizing where you are failing and starting to change. Each takes the hints as you wish.

Browser other questions tagged

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