That doesn’t make much sense. A ArrayList
of avaliacaoMensal
may only contain objects of the type avaliacaoMensal
. You need to create an instance of this class and add the element to the list.
public avaliacaoMensal todosMensal(){
List<avaliacaoMensal> dados = new ArrayList<avaliacaoMensal>();
avaliacaoMensal aMensal = new avaliacaoMensal();
// Fazer algo para setar o valor dos atributos questao, potencial e resultado
dados.add(aMensal);
}
Note that the current code of the class avaliacaoMensal
does not contain the methods of access to its properties (the famous getters
and setters
), you need to create them.
It is also good to follow the Java nomenclature standard for class names (Camel case with the first uppercase letter), the class should be called AvaliacaoMensal
.
A very common practice is also the creation of constructors that receive some attribute values as a parameter, to create an instance of the class with certain values.
Your class, following these tips, would look like this:
public class AvaliacaoMensal {
private String questao;
private char potencial;
private int resultado;
public AvaliacaoMensal(String questao, char potencial, int resultado) {
this.questao = questao;
this.potencial = potencial;
this.resultado = resultado;
}
public String getQuestao() {
return questao;
}
public void setQuestao(String questao) {
this.questao = questao;
}
public char getPotencial() {
return potencial;
}
public void setPotencial(char potencial) {
this.potencial = potencial;
}
public int getResultado() {
return resultado;
}
public void setResultado(int resultado) {
this.resultado = resultado;
}
}
That way, you could use similar to what you tried in the beginning:
public AvaliacaoMensal todosMensal(){
List<AvaliacaoMensal> dados = new ArrayList<AvaliacaoMensal>();
dados.add(new AvaliacaoMensal("Aaa", 'a', 1));
// Continuação do código
}
You can see running on repl.it.
The question is: how to start the object directly in the method
add
of the list?– Renan Gomes
That’s right @Renan
– Lucas Charles
@Lucascharles I put at the end of my answer how you can do it.
– Jéf Bueno
@jbueno Thank you very much, you are saving me. But you are giving the following error: Evaluatesomensal() in Evaluatesomensal cannot be Applied to: Expected Parameters: Actual Arguments:
– Lucas Charles
I need to see the code to know what you’re talking about
– Jéf Bueno
@jbueno Editei la
– Lucas Charles
Let’s go continue this discussion in chat.
– Lucas Charles