【问题标题】:I'm trying to search an ArrayList of objects for duplicates and if it doesnt exist add the new object我正在尝试在对象的 ArrayList 中搜索重复项,如果不存在则添加新对象
【发布时间】:2016-05-15 16:37:59
【问题描述】:

我有一个名为 Subject 的类,它将存储一些信息,如主题名称和主题代码,我正在努力,因为我想做的是让它遍历 arraylist 中的记录列表并添加新记录,如果它尚不存在。请帮忙,我已经尝试在这里搜索答案,但似乎无法找到它。

如果循环不是正确的做法,请指出正确的方向。 谢谢

//The Class

public class Subject {
    private String name;
    private String subjectCode;

    public Subject(){    
    }

    public Subject(String name, String subjectCode){
        this.name = name;
        this.subjectCode = subjectCode;
    }

    public String getName(){
        return this.name;
    }

    public void setName(String name){
        this.name = name;
    }

    public String getSubjectCode(){
        return this.subjectCode;
    }

public void setSubjectCode(String subjectCode){
    this.subjectCode = subjectCode;
}


//The Main method

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    ArrayList<Subject> subjectList = new ArrayList<>();

    //Test records
    subjectList.add(new Subject("Java 1", "ITC101"));
    subjectList.add(new Subject("Java 2", "ITC201"));
    subjectList.add(new Subject("Java 3", "ITC301"));


    String newGetName;
    String newSubjectCode;


    do {
        System.out.print("Enter Subject Name: ");
        newGetName = input.nextLine();

        System.out.print("Enter Subject Code: ");
        newSubjectCode = input.nextLine();

        for(int i = 0; i < subjectList.size(); i++){
            if(!subjectList.get(i).getName().contains(newGetName) && !subjectList.get(i).getSubjectCode().contains(newSubjectCode)){
            subjectList.add(new Subject(newGetName, newSubjectCode));
            } else {
                    System.out.println("We have a match ");

            }
        }
    } while(!"0".equals(newGetName));


}

【问题讨论】:

  • 如果您需要一个没有重复的容器,请考虑使用Set
  • 您需要遍历整个subjectList 以检查是否已经包含newGetName(因为当当前subject 不匹配时,您将多次添加它)。考虑覆盖subject 中的等于并使用Set
  • 你的类应该提供适当的 equals 方法进行比较。

标签: java arraylist


【解决方案1】:

您过早宣布“不匹配”:您需要在添加新主题之前遍历整个列表。

您的程序尝试第一个主题,如果 ut 不匹配,它会添加一个主题,然后继续。不幸的是,它并没有中断,因此它不断为每个不匹配的现有主题添加相同的主题。

为了解决这个问题,创建一个名为“found”的布尔变量,在循环之前将其设置为 false,然后搜索匹配项。找到匹配项后,将变量设置为 true,然后中断。

循环后检查你的变量。如果是真的,请不要添加,并说您找到了重复项。否则,添加新主题。

【讨论】:

    【解决方案2】:

    在您的情况下,您无法检查“包含”,您必须手动检查每个条目是否相同。

    覆盖主题的 equal() 函数:

    public boolean equal(Object o){
       Subject b = (Subject) o;
       return (this.name.equal(b.name) && this.code.equal(b.code));
    }
    

    然后在你的循环中:

    boolean found = false;
    for(Subject s : subjectList){
       if(s.equal(newSubject)){
          found = true;
          break;
       }
    }
    if(!found) //add new entry
    

    【讨论】:

    • 你是对的,包含等于... 为什么投反对票?我想,这就是 TO 想要的?
    【解决方案3】:

    如果Subject 实现equals(),则创建对象并调用subjectList.contains(subject)。更好的是,也实现hashCode() 并将ArrayList 更改为HashSet(或LinkedHashSet,如果顺序很重要)以获得更好的性能。

    否则,像这样搜索(使用equals(),而不是contains(),用于字符串比较):

    boolean found = false;
    for (Subject subject : subjectList)
        if (subject.getName().equals(newGetName) && subject.getSubjectCode().equals(newSubjectCode)) {
            found = true;
            break;
        }
    if (found)
        System.out.println("We have a match ");
    else
        subjectList.add(new Subject(newGetName, newSubjectCode));
    

    【讨论】:

      猜你喜欢
      • 2016-01-07
      • 2012-02-28
      • 2020-03-21
      • 2016-12-20
      • 2012-04-24
      • 2020-12-14
      • 1970-01-01
      • 2017-12-15
      相关资源
      最近更新 更多