【问题标题】:Error with method contains Vector java方法错误包含 Vector java
【发布时间】:2016-10-11 16:53:58
【问题描述】:

我想制作电话簿。

使用“contains(OBJECT)==TRUE”方法在“while”循环中无法识别执行和放置两个相同的对象。

我的代码哪里出错了?感谢任何帮助谢谢!

主要

public class MainRubrica {

    public static void main(String[] args) {
        Scanner keyb= new Scanner(System.in); 

        System.out.print("Inserire il numero di contatti da aggiungere: ");
        int nM= keyb.nextInt(); 

        Vector<Contatto> rubrica = new Vector<Contatto>(20, 5);

        for(int i=0;i<nM;i++){
            System.out.println("\nContatto n."+(i+1));
            Contatto c =new Contatto();
            c.inserimento();
            while(rubrica.contains(c)==true) {
                System.out.println("Il contatto è già presente");
                c.inserimento();
            }
            rubrica.addElement(c);
        }

        for(int i=0;i<nM;i++){
            System.out.println("\nContatto n."+(i+1));
            System.out.println(rubrica.elementAt(i));
        }

班级联系方式

public class Contatto {
    //attributi
    private String nome; 
    private String cognome;
    private String numeroTel;

    //costruttore di default
    public Contatto(){
        nome=""; 
        cognome="";
        numeroTel=""; }

    //costruttore con parametri
    public Contatto(String nome, String cognome, String numeroTel){
        this.nome=nome; 
        this.cognome=cognome; 
        this.numeroTel=numeroTel; }

    //metodo set
    public void setNome(String nome){
        this.nome=nome; }
    public void setCognome(String congnome){
        this.cognome=cognome; }
    public void setNumeroTel(String numeroTel){
        this.numeroTel=numeroTel; }

    //metodo get
    public String getNome(){
        return nome; }
    public String getCognome(){
        return cognome; }
    public String getNumeroTel(){
        return numeroTel; }

    //metodo inserimentoContatto
    public void inserimento(){
        Scanner keyb= new Scanner(System.in); 
        System.out.println("Nome: ");
        nome=keyb.nextLine(); 
        System.out.println("Cognome: ");
        cognome=keyb.nextLine();  
        System.out.println("Numero di telefono: ");
        numeroTel=keyb.nextLine(); 
    }

    public String toString(){
        return "Nome: "+nome+"\nCognome: "+cognome+"\nNumero di Telefono: "+numeroTel; 
    }

}

【问题讨论】:

标签: java vector contains


【解决方案1】:

我不明白你想要完成什么,但你没有进入 while 循环的原因是你没有覆盖你的 Contatto 类的 Object.equals 方法。下面是 Object.equals 的定义:

public boolean equals(Object obj) {
    return (this == obj);
}

使用“equals”方法的这种定义,所有字段是否具有等效值并不重要。这两个对象不相等,除非它们是同一个对象。

在你的 Contatto 类中重写 equals 方法将解决这个问题。当您这样做时,您还需要重写 Object.hashCode 方法,以便也维护该方法的协定(相同的对象必须具有相同的哈希码)。我喜欢为此使用Apache Commons Lang 库。使用该库,您可以将这些方法添加到您的 Contatto 类中:

@Override
public boolean equals(final Object obj) {
    if (this == obj) {
        return true;
    } else if (obj instanceof Contatto) {
        final Contatto rhs = (Contatto) obj;
        return new EqualsBuilder().append(getNome(), rhs.getNome())
                .append(getCognome(), rhs.getCognome())
                .append(getNumeroTel(), rhs.getNumeroTel())
                .isEquals();
    } else {
        return false;
    }
}

@Override
public int hashCode() {
    return new HashCodeBuilder().append(getNome()).append(getCognome())
            .append(getNumeroTel()).toHashCode();
}

没有 Apache Commons Lang 库(应该等同于库在做什么):

@Override
public boolean equals(final Object obj) {
    if (this == obj) {
        return true;
    } else if (obj instanceof Contatto) {
        final Contatto rhs = (Contatto) obj;
        return Objects.equals(getNome(), rhs.getNome())
                && Objects.equals(getCognome(), rhs.getCognome())
                && Objects.equals(getNumeroTel(), rhs.getNumeroTel());
    } else {
        return false;
    }
}

@Override
public int hashCode() {
    int hash = 17 * 37 + getNome().hashCode();
    hash = hash * 37 + getCognome().hashCode();
    hash = hash * 37 + getNumeroTel().hashCode();
    return hash;
}

添加这些方法或它们的实现与您对两个 Contatto 实例之间的相等定义相匹配后,如果您输入等效的 Contatto 对象,您应该会看到您的代码进入了该 while 循环。

【讨论】:

  • 谢谢!但是我总是在 equalsBuilder() 和 HashCodeBuilder() 上出错。我必须导入一些东西?
  • 是的。要使用此特定解决方案,您需要在构建中包含 Apache Commons Lang 库,并且需要导入 org.apache.commons.lang3.builder.EqualsBuilderorg.apache.commons.lang3.builder.HashCodeBuilder。如果你不想使用这个库,这里有很多关于如何正确编写 equalshashCode 方法的信息。
  • 你能给我一个不使用这个库的例子吗谢谢?
  • 编辑添加了一个没有 Apache Commons Lang 库的示例。
猜你喜欢
  • 1970-01-01
  • 2014-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-17
  • 2013-03-29
  • 2023-03-23
相关资源
最近更新 更多