How to create a list with some attributes in java?

Asked

Viewed 8,760 times

1

I’m having trouble creating a list where I can enter different values.

I use the following codes:

public class avaliacaoMensal {
    private String questao;
    private char potencial;
    private int resultado;
}

public avaliacaoMensal todosMensal(){    
    List<avaliacaoMensal> dados = new ArrayList<avaliacaoMensal>();
    dados.add("Aaa", 'a', 1);    
}

How to make it work?

public AvaliacaoMensal todosMensal(){
        List<AvaliacaoMensal> dados = new ArrayList<AvaliacaoMensal>();
        dados.add(new AvaliacaoMensal("Aaa", 'a', 1));
        dados.add(new AvaliacaoMensal("Bbb", 'b', 2));
        dados.add(new AvaliacaoMensal("Ccc", 'c', 3));
        // Continuação do código

        return (AvaliacaoMensal) dados;
    }

Then when I add, it generates the error

Avaliacaomensal() in Avaliacaomensal cannot be Applied to: Expected Parameters: Actual Arguments

  • The question is: how to start the object directly in the method add of the list?

  • That’s right @Renan

  • @Lucascharles I put at the end of my answer how you can do it.

  • @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:

  • I need to see the code to know what you’re talking about

  • @jbueno Editei la

Show 2 more comments

2 answers

3


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.

1

You need to do it this way.

Avaliacaomensal.java will have this code:

public class AvaliacaoMensal {
private String questao;
private char potencial;
private int 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;
}

}

Now you can take a test class and do this to see if it works :

    ArrayList<AvaliacaoMensal> lista = new ArrayList<AvaliacaoMensal>();

    AvaliacaoMensal avaliacao = null;
    for (int i = 0; i < 3; i++) {
        avaliacao = new AvaliacaoMensal();
        avaliacao.setPotencial('T');
        avaliacao.setQuestao("Teste");
        avaliacao.setResultado(2);
        lista.add(avaliacao);
    }

Browser other questions tagged

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