Create Object Array in java and access objects directly

Asked

Viewed 20,770 times

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

  • 1

    what do you mean by queria poder acessa-los diretamente?

  • 2

    You don’t need to put in a temporary object. Just know the position and do so. arrayList.get(i). getPropriation().

  • @Math access from the mode the adelmo00 showed

  • @adelmo00 has tried so and it doesn’t work, I can’t access neither the attributes nor the methods

  • 1

    @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.

  • 2

    @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 Exactly face set now the Arraylist as Arraylist<Blocoview> ran straight thanks to all

  • @alleen94 I will add an answer, not just as a comment.

  • beauty @utluiz ja seleciono ela como aceita

Show 4 more comments

1 answer

3


To do this you must define the generic type of ArrayList, thus:

public class MemoriaView extends JPanel {
    private ArrayList<BlocoView> listadeBlocos;
    public MemoriaView(){
        super();
        this.listadeBlocos = new ArrayList<BlocoView>();
        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));
    }

}

Without this, the list type is Object, and even though the instance is the type you passed, you will not have access to the methods directly.

Another alternative is to make a cast, thus:

BlocoView bloco = (BlocoView) this.listadeBlocos.get(0);
this.add(bloco.getEndereco());

Browser other questions tagged

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