2016-03-03 54 views
0

我試圖從鏈表中刪除一個實例,但是當我嘗試搜索列表中的對象時,它返回值-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 + ")"; 
    } 


} 

回答

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; 
    } 

在上面的刪除方法中,您正在創建一個新的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; 
    } 
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; 
} 
相關問題