从使用对的链接列表中删除实例

时间:2016-03-03 12:51:09

标签: java linked-list

我正在尝试从链接列表中删除一个实例,但是当我尝试在列表中搜索对象时,它返回的值为-1,因为它表示它不在那里。我究竟做错了什么。我的应用程序类在下面,它调用我的DataSet类中的方法

public class Application {

    public static void main(String[] args) {

        DataSet<String, Integer> db = new DataSet<>();


        db.add("Theo", 4);
        db.add("Maria", 5);
        db.add("Adam", 4);
        db.add("James", 5);
        db.add("Charles", 7);
        db.add("Nikki", 5);
        db.add("Lynne", 5);
        db.add("Kendal", 6);
        db.add("Kerry", 5);
        db.add("Janet", 5);
        db.add("Gordon", 6);
        db.add("Stepher", 7);
        db.add("Sue", 3);
        db.add("Ed", 2);
        db.add("Adam", 4);


        db.displayItems();
  /*      
        System.out.println();

        db.sortByFirst();
        db.displayItems();

        System.out.println();

        db.sortBySecond();
        db.displayItems();

        System.out.println();

        (db.findBySecond(5)).displayItems();

        System.out.println();

       (db.findByFirst("Adam")).displayItems();

        System.out.println();

       */ System.out.println(db.remove("Adam", 4));
        db.displayItems();
        //System.out.println("size = " + db.size());

    }

}

,数据集为:

import java.util.LinkedList;


/**
 *
 * @param <T>
 * @param <S>
 */
public class DataSet<T, S> {

    LinkedList<Pair> datastructure = new LinkedList<>();
// Adds a new instance/item to the data structure.

    public void add(T first, S second) {
        Pair p = new Pair(first, second);
        datastructure.add(p);
    }
    // Displays all itmes in the data structure.

    public void displayItems() {

        for (int i = 0; i < datastructure.size(); i++) {
            System.out.println(datastructure.get(i));
        }
    }
    // Removes all instances with matching criteria (first and second attribute values) and returns the number of instances removed.

    public int remove(T first, S second) {
        int count = 0;
        Pair p = new Pair(first, second);

        for (Pair datastructure1 : datastructure) {
            Integer num = datastructure.indexOf(p);
            System.out.println(num);
            Boolean removed = datastructure.remove(p);
            System.out.println(removed);
        }

        //will return count of how many removed
        return count;
    }

}

,最终的类是对类

class Pair<T,S> {

     private T first;
    private S second;

    public Pair(T theFirst, S theSecond) {
        first = theFirst;
        second = theSecond;
    }

    public T getFirst() {
        return first;
    }

    public S getSecond() {
        return second;
    }

    @Override
    public String toString() {
        return "(" + first + ", " + second + ")";
    }


}

2 个答案:

答案 0 :(得分:0)

public int remove(T first, S second) {
        int count = 0;
        Pair p = new Pair(first, second);

        for (Pair datastructure1 : datastructure) {
            Integer num = datastructure.indexOf(p);
            System.out.println(num);
            Boolean removed = datastructure.remove(p);
            System.out.println(removed);
        }

        //will return count of how many removed
        return count;
    }

在上面的remove方法中,您将创建一个新的Pair对象。新对象意味着新引用,因此datastructure.indexOf(p)将始终为-1。

Example:
datastructure contains three pairs:
Pair1 - reference 0x00000001 - "Theo",4
Pair2 - reference 0x00000002 - "Theo",5
Pair3 - reference 0x00000003 - "Theo",6 

And we asked to remove "Theo",4. So `p` will be a new object like:
p - reference 0x00000004 - "Theo",4

这意味着p的引用不匹配,也不会检查数据。修改Pair类的equals方法,如下所示:

@Override
    public boolean equals(Object obj) {
        if(this == obj)
            return true;
        if(obj instanceof Pair)
        {
            Pair pair = (Pair)obj;
            if(pair.first.equals(this.first) && pair.second.equals(this.second)){
                return true;
            }
        }
        return false;
    }

答案 1 :(得分:0)

就像亚当指出的那样,问题是你正在创建一个不在列表中的新对。你想要做的是在你的Pair类中创建一个equals方法然后迭代你的列表使用这个equals方法比较元素。该方法应如下所示:

@Override
public boolean equals(Object obj) {
    if (obj == null) {
        return false;
    }
    if (getClass() != obj.getClass()) {
        return false;
    }
    final Pair other = (Pair) obj;
    if (this.first != other.first) {
        return false;
    }
    if (this.second != other.second) {
        return false;
    }
    return true;
}