-1
I created a class Pessoa
and in it an attribute endereco
, only I made a class Endereco
to register more than one endereco
per person.
In that other class Endereco
has id
and nome da rua
, and I can’t manipulate them from class Pessoa
.
I want to save you both together in one array.
public static void main(String[] args) {
ArrayList<Pessoa> pessoa = new ArrayList<>();
Pessoa c1 = new Pessoa();
c1.setId(1);
c1.setNome("Pedro");
c1.setEndereco(endereco);//Erro, nao consigo manipular para por o nome da rua e nem o id
System.out.println(c1.getEndereco());
pessoa.add(c1);
}
public class Pessoa {
private int id;
private String nome;
private Endereco endereco;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public Endereco getEndereco() {
return endereco;
}
public void setEndereco(Endereco endereco) {
this.endereco = endereco;
}
}
public class Endereco {
private int id;
private String rua;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getRua() {
return rua;
}
public void setRua(String rua) {
this.rua = rua;
}
}
You declare the variable
endereco
where in the main function? You wouldn’t have to create an address Arraylist and a Person object?– Edward Ramos
Like, I want an array that can register several addresses if needed. Plus the Address that is in another class can be saved in the right sequence of the person array.
– Pedro
Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site
– Maniero