How to declare list in class?

Asked

Viewed 87 times

1

I have a class Usuario you will have a list of the class Pastas, and this class will have a list of the class Questão.

public class Usuario{
     String nome; 
     ArrayList< Pasta > listaPasta;
}

My problem is not knowing if that placement is right, because in my head, I want to create the class Usuario and have the list created, or if I have to create on the outside after the creation of the class and give a SetListaPasta().

  • Use formatting for code

  • Every user created already has some folder?

  • Yes dherik, that would be the idea. And I managed to reach it.

1 answer

1


We have no way of knowing why you don’t have more information on the question. Programming is less writing code and more understanding the problem and knowing what to do (not how to do).

It seems to me that it is correct like this, I should only initialize the list, in the current form it is null. Of course you can initialize in the constructor as well.

You may already put the value of the list in the constructor, or you can add the items to it.

It is possible to have a SetListaPasta(), but almost always this is a mistake, it would make some sense to have a method that adds folders in the list. But it might not be ideal. Depends on the requirement we don’t know.

Everything is valid according to need.

  • I can’t get the stack right yet, it’s my first question, but obg by the answer!

  • I kept doing separate tests of my code and solved as follows: I placed the instantiation inside the constructor and made a method setListPasta(material), which received an object to be added in the list. And within the method was this way: listPasta. Add(material).

Browser other questions tagged

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