AJBM Posted April 19, 2012 at 06:26 PM Report #450037 Posted April 19, 2012 at 06:26 PM Boas! eu tenho que fazer um metodo para remover um contacto so que nao estou a conseguir fazer alguem me pode ajudar public class Contacto { private String nome; private int numero_tel; private String mail; Contacto(String nome,int numero_tel){ this.nome=nome; this.numero_tel=numero_tel; } Contacto(String nome,int numero_tel,String mail){ this.nome=nome; this.numero_tel=numero_tel; this.mail=mail; } public String getMail() { return mail; } public void setMail(String mail) { this.mail = mail; } public String getNome() { return nome; } public void setNome(String nome) { this.nome = nome; } public int getNumero_tel() { return numero_tel; } public void setNumero_tel(int numero_tel) { this.numero_tel = numero_tel; } } public class Lista { private Contacto[] contacto; public Lista(Contacto[] contacto) { this.contacto = contacto; } public Contacto[] getContacto(){ return contacto; } public void setContacto(Contacto[] contacto) { this.contacto = contacto; } public int capacidadeLista(){ return contacto.length; } public void listarContactos(){ for(int i=0;i<contacto.length;i++){ System.out.println(contacto[i].getNome()+"\n"+contacto[i].getNumero_tel()+"\n"+contacto[i].getMail()); } } } Lista lista; lista = new Lista(new Contacto[]{new Contacto("jj",3)}); lista.listarContactos(); System.out.println("Capacidade: "+lista.capacidadeLista());
KTachyon Posted April 19, 2012 at 10:18 PM Report #450070 Posted April 19, 2012 at 10:18 PM Não vou responder à tua dúvida, porque, de facto, nem estou a ver o teu código a funcionar. Mas, não seria mais prático utilizares um ArrayList em vez de criares um objecto Lista que contém um array de contactos? ArrayList<Contacto> listaDeContactos = new ArrayList<Contacto>(); listaDeContactos.add(new Contact("jj", 3); System.out.print("A lista tem " + listaDeContactos.size() + " contactos."); Para remover, sabendo a posição do teu contacto: listaDeContactos.remove(posicao); Alterantivamente, se quiseres ter os contactos indexados por nome, para facilitar a remoção podes utilizar um HashMap: HashMap<String, Contacto> listaDeContactos = new HashMap<String, Contacto>(); listaDeContactos.put("jj", new Contact("jj", 3); System.out.print("A lista tem " + listaDeContactos.size() + " contactos."); // remover listaDeContactos.remove("jj"); “There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult.” -- Tony Hoare
AJBM Posted April 20, 2012 at 07:04 PM Author Report #450276 Posted April 20, 2012 at 07:04 PM Boas! O problema nao posso utilizar arraylist e nem o hashmap
KTachyon Posted April 20, 2012 at 07:10 PM Report #450278 Posted April 20, 2012 at 07:10 PM Mas também não podes utilizar arrays sem as inicializares com um valor de casas fixo. “There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult.” -- Tony Hoare
AJBM Posted April 21, 2012 at 12:01 PM Author Report #450376 Posted April 21, 2012 at 12:01 PM ja consegui 😛 fica aqui a resolucao public boolean delete(int index) { boolean deleted = false; if (index >= 0 && index < contacto.length) { Contacto[] tmp = new Contacto[contacto.length - 1]; for (int i = 0; i < index; i++) { tmp[i] = contacto[i]; } for (int i = index; i < tmp.length; i++) { tmp[i] = contacto[i + 1]; } contacto = tmp; deleted = true; } return deleted; }
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now