How do I access the indexes of a vector returned by a method in java?

4

I have a method called vetorVoos that returns a vector of type NodeVoo and I want to access the contents of that vector through the method in another class.

This is the method:

public NodeVoo[] vetorVoos(){
    if(isEmpty()){
        return null;
    }
    NodeVoo vetor[] = new NodeVoo[size()],aux2 = inicio;
    for (int i = 0; i < size(); i++) {
        vetor[i] = aux2;
        aux2 = aux2.proximo;
    }
    return vetor;
}

And I already tried to get the vector index in the following ways in another class:

vetorVoos()[1],
vetorVoos(1),
[1]vetorVoos()

None of this worked.

    
asked by anonymous 04.05.2014 / 01:03

2 answers

2

First, store the function return in a variable:
NodeVoo[] voos = vetorVoos(); Home and then accesses the indexes: NodeVoo voo = voos[1];

    
04.05.2014 / 02:30
0

Create a class NodeVoo that will be your class template for a future list:

NodeVoo Class (example)

public class NodeVoo {    
    private String descricao;
    public String getDescricao() {
        return descricao;
    }
    public void setDescricao(String descricao) {
        this.descricao = descricao;
    }    
}

After creating the class NodeVoo create a list of NodeVoo ( Array ) with the name of ListaVoo as the code just below:

Class ListVoo (example)

import java.util.ArrayList;
import java.util.List;
public class ListaVoo {
    public ListaVoo(){
        this.nodeVoo = new ArrayList<>();
    }    
    public ListaVoo(List<NodeVoo> nodeVoo){
        this.nodeVoo = nodeVoo;
    }
    private List<NodeVoo> nodeVoo;
    public List<NodeVoo> getNodeVoo() {
        return nodeVoo;
    }
    public void setNodeVoo(List<NodeVoo> nodeVoo) {
        this.nodeVoo = nodeVoo;
    }    
    public void addVoo(NodeVoo nodeVoo){
        this.nodeVoo.add(nodeVoo);
    }    
}

Using

    //Criando 2 instâncias da classe NodeVoo (Modelo)
    NodeVoo nodeVoo1 = new NodeVoo();
    NodeVoo nodeVoo2 = new NodeVoo();

    //Criando a lista de ListaVoo
    ListaVoo lista = new ListaVoo();

    //Adicionando na lista as duas classes NodeVoo
    lista.addVoo(nodeVoo1);
    lista.addVoo(nodeVoo2);

    //Resgatando a quantidade de itens na lista ListaVoo
    int quantidade = lista.getNodeVoo().size();

All data are examples, but fully functional.

    
04.05.2014 / 02:09