I have two arraylist like this
ArrayList<Paragens> paragens = new ArrayList<Paragens>();
ArrayList<Rotas> rotas = new ArrayList<Rotas>();
Paragens is a class and the constructor is
public Paragens(String nomeParagem) {
this.nomeParagem = nomeParagem;
this.existeNaRota = new String[]{"","","","","","","","","","","","","",
"","","","","","","","","","","","","","","","","","","","","","","","",
"","","","","","","","","","","","","","","","","","","","","","","","",
"","","","","","","","","","","","","","","","","","","","","","","",""};
this.id = contadorParagens++;
}
I have another class. The Rotas class.
public Rotas(String nomeRota, Paragens[] nomeParagens) {
this.nomeRota = nomeRota;
this.nomeParagens = nomeParagens;
this.id = contadorRotas++;
}
So there are Paragens
(each one with a name[nomeParagem
], an id[contadorParagens
] and the rotas in which they exist [existeNaRota
].
First, the user inputs Paragens
which are only created with nomeParagem
(it's name) and its id
. Since Rotas are added latter, existeNaRota
it's empty.
Each Paragem
is then added to paragens
arraylist
When user adds an Rotas
he inputs Rota
name and then the list of Paragens
that exist on that Rotas
. Rotas are finally added to
rotas` arraylist.
What I want to do is, when user insert Paragens
that exists on one Rotas
, existeNaRota
is them populated with the Rotas
that Paragem
exists.
Example.
User inputs some names and then Paragens
are created like this:
paragem = new Paragens("London");
paragens.add(paragem);
paragem = new Paragens("Manchester");
paragens.add(paragem);
paragem = new Paragens("Chelsea");
paragens.add(paragem);
Now user will input Rotas
like this:
estacoes[0] = "Chelsea";
estacoes[1] = "London";
rota = new Rotas("Route A", estacoes);
rotas.add(rota);
Now, since there is a Rota
I want to go back to Paragens
and put the name on existeNaRota
.
On Paragens
I have this set method
public void setExisteNaRota(String existeNaRota, int indice) {
this.existeNaRota[indice] = existeNaRota;
indice++;
}
Is it possible to access the paragens
arraylist, find the "Chelsea" Paragem
and add "Rota A" to the first position of existeNaRota
?
First, consider using a List
in `Paragens' instead of an array since you are going to be adding elements to it. This will allow for adding (potentially to the head of the list) without moving elements in the array.
Second, provide a method in Paragens
like addRota(Rota r)
that adds the Rota
to the List
of Rotas
in the Paragens
.
class Paragens{
List<Rota> existEnRota = new ArrayList<Rota>();
public void addRota(Rota r){
existEnRota.add(0, r);
}
}