1
So I need to do the following I have a class with some attributes and methods, I need to generate a array
of objects of this class, however I wanted to be able to access them directly, currently what I managed to do is to throw them in a arraylist
and use the get(posição)
to assign them in a temporal variable to be able to access their methods and attributes.
Class I want to create an object array
public class BlocoView {
private MemoriaTextField enderecoMemoria;
private MemoriaTextField palavras;
public BlocoView(boolean selected){
if(selected){
this.enderecoMemoria = new MemoriaTextField("0", true);
this.palavras = new MemoriaTextField("???", true);
}else{
this.enderecoMemoria = new MemoriaTextField("0", false);
this.palavras = new MemoriaTextField("???", false);
}
}
public MemoriaTextField getEndereco(){
return this.enderecoMemoria;
}
}
Class containing the arraylist of objects of the above class
public class MemoriaView extends JPanel {
private ArrayList listadeBlocos;
public MemoriaView(){
super();
this.listadeBlocos = new ArrayList();
this.listadeBlocos.add(new BlocoView(false));
this.setLayout(new MigLayout());
//Aqui que tenho que acessar os atributos ou metodos diretamente
this.add(this.listadeBlocos.get(0));
}
}
Trying to do something like
this.add(this.listadeBlocos.get(0).getEndereco);
java cannot find the get address method
what do you mean by
queria poder acessa-los diretamente
?– Math
You don’t need to put in a temporary object. Just know the position and do so. arrayList.get(i). getPropriation().
– adelmo00
@Math access from the mode the adelmo00 showed
– tissei
@adelmo00 has tried so and it doesn’t work, I can’t access neither the attributes nor the methods
– tissei
@alleen94 if you are able to assign a class type variable for sure you can directly access tb, as adelmo00 said. You must be making some other mistake along the way, if you can’t solve post the code you’re struggling with.
– Math
@alleen94 I believe the problem of OP is that it did not define a type for the list, using Generics, so it only sees an Object when giving the get.
– utluiz
@utluiz Exactly face set now the Arraylist as Arraylist<Blocoview> ran straight thanks to all
– tissei
@alleen94 I will add an answer, not just as a comment.
– utluiz
beauty @utluiz ja seleciono ela como aceita
– tissei