簡體   English   中英

LinkedList indexOf方法檢查

[英]LinkedList indexOf method check

我目前正在從頭開始構建一個鏈表,並且一切正常,除了在我的測試驅動程序上,當我嘗試獲取某個對象的索引時,它給我的值似乎減少了2個值,(應為3,其5)

測試驅動

public static void main ( )
{   List<String> friends = new LinkedList <String> ();

    System.out.println (" Testing problem 1");
friends.add ("joe");
friends.add ("mary");
friends.add ("jim");
friends.add ("joe");                            // Lists may contain duplicate elements
friends.add (2, "sally");                       // Insert at position 2
System.out.println (friends.get(3));            // Should be jim
friends.remove (0);
System.out.println (friends.indexOf ("joe"));   // Should be 3
String sal = new String ("sa" + "lly");         
if (! friends.contains(sal))                    // "sally"
    System.err.println ("Not correct");

我的indexOf方法

 /**
 * 
 * @returns index value, -1 if object was not found
 */
public int indexOf(Object obj) {

     int index = 0;
    Node<E> current = head.next;
    while (current != null) {
        if (current.equals( obj)) {
            return index;
        }

        index++;
        current = current.next;
    }
    if(index == size && obj == null){
        return -1;
    }
    else{
        return index;
    }
}

我的add(E value)方法

public void add(E value){
    Node<E> temp = new Node<E>(value,tail,tail.previous);
    tail.previous = temp;
    temp.previous.next = temp;
    size++;

}

我的add(int index,E value)方法

public void add(int index, E value) {
   setRef(index);
  Node<E> temp = new Node<E>(value,ref,ref.previous);
  temp.previous.next = temp;
  ref.previous = temp;
  size++;
}

任何想法,為什么我可能會得到5時應該是3的索引?

對於那些想要完整資料的人

LinkedList.java

package list;


public class LinkedList<E> implements List<E>
{
    //size of list
    int size = 0;
    Node<E> head;
    Node<E> tail;
    Node<E>ref;
    public LinkedList(){
        head = new Node<E>(null,null,null);
        tail = new Node<E>(null,null,head);
        head.next = tail;
    }

    public void add(E value){

        Node<E> temp = new Node<E>(value,tail,tail.previous);
        tail.previous = temp;
        temp.previous.next = temp;
        size++;

    }

    public E get(int index){
     setRef(index);
     return ref.value;
    }

    public E set(int index, E value){
        setRef(index);
        E result = ref.value;
        ref.value = value;
        return result;
    }

    public void add(int index, E value) {
       setRef(index);
      Node<E> temp = new Node<E>(value,ref,ref.previous);
      temp.previous.next = temp;
      ref.previous = temp;
      size++;
    }


    public int size(){
        return size;
    }

    public E remove(int index){
      setRef(index);
        E result = ref.next.value;
        ref.previous.next = ref.next;
        ref.next.previous = ref.previous;
        size --;
        return result;
    }

    public void clear(){
       size = 0;
       head.next = tail;
       tail.previous = head;
       ref = null;

    }

    /**
     * @returns true if size of list is equal to 0
     */
    public boolean isEmpty(){
        if(size == 0 ){
            return true;
        }
        else{
            return false;
        }
    }

    private void setRef(int index){
    ref = head.next;
    for(int i = 0; i < index; i++){
    ref = ref.next;
    }

    }

    /**
     * 
     * @returns index value, -1 if object was not found
     */
    public int indexOf(Object obj) {

         int index = 0;
        Node<E> current = head.next;
        while (current != null) {
            if (current.equals( obj)) {
                return index;
            }

            index++;
            current = current.next;
        }
        if(index == size && obj == null){
            return -1;
        }
        else{
            return index;
        }
    }

    /**
     * @returns true if list contains Object obj
     */
    public boolean contains(Object obj){
        boolean isTrue;
        if(!(this.indexOf(obj) == -1)){
            isTrue = true;
        }
        else{
            isTrue = false;
        }
        return isTrue;

    }

    public String toString() {
        String result = "[";
        Node<E> current = head;
        while(current.next != null){

            current = current.next;
            result += current.value + ", ";

        }
        return   result += "]";
    }

    public RefIterator<E> Riterator(){
    return new RefIterator<E>(this);
    }

    public ArrayIterator<E>iterator(){
    return new ArrayIterator<E>(this);
    }
    public  ListIterator<E> listIterator(){
    return new RefListIterator<E>(this);
    }

    public  ListIterator<E> listIterator(int start){
    return new RefListIterator<E>(this,start);
    }

    }

List.java

public interface List<E> 
{
    /**
     * @return the size of this list
     */
    int size(); // all methods in interfaces must be public

    /**
     * Clear this list
     */
    void clear();

    /**
     *@return the value at a given position by index 
     */
    E get(int index); // index must be in bounds -- check for that later

    /**
     * @returns the value that is being replaced
     */
    E set(int index, E value);

    /**
     * Insert a given value at the position index
     */
    void add(int index, E value);

    /**
     * Inserts a given value at the end of index
     */
    void add(E value);

    /**
     * @returns true if List is empty
     */
    boolean isEmpty();

    /**
     * removes value at the given index
     * @return value being removed
     */
    E remove(int index);

    int indexOf(Object obj);

    boolean contains(Object obj);

    String toString();

    /**
     * @return refrence to the iterator
     */
    ArrayIterator<E>iterator();

    RefIterator<E> Riterator();

    ListIterator<E> listIterator();

    ListIterator<E> listIterator(int start);

}

測試驅動

    package list;
import list.*;

/**
 * Lab 3
 * Test methods added to the List interface
 * 
 * @author (sdb) 
 * @version (Sep 2015)
 */
public class DriverLab01PM
{
/**
 *  This main method tests the List classes
 *  for lab 1, Data Structures and Algorithms
 */
    public static void main ( )
    {   List<String> friends = new LinkedList <String> ();

        System.out.println (" Testing problem 1");
    friends.add ("joe");
    friends.add ("mary");
    friends.add ("jim");
    friends.add ("joe");                            // Lists may contain duplicate elements
    friends.add (2, "sally");                       // Insert at position 2
    System.out.println (friends.get(3));            // Should be jim
    friends.remove (0);
    System.out.println (friends.indexOf ("joe"));   // Should be 3
    String sal = new String ("sa" + "lly");         
    if (! friends.contains(sal))                    // "sally"
        System.err.println ("Not correct");


//////////// Uncomment the following when ready for problem 2
        System.out.println ("\n\n Testing problem 2");   
        System.out.println (friends);                   // [mary, sally, jim joe]
        List <String> enemies = null;
        System.out.println (enemies);
        enemies = new ArrayList<String> ();
        System.out.println (enemies);
        enemies.add ("mike");
        enemies.add ("mick");
        System.out.println (enemies);
        if (! enemies.contains ("mike"))
            System.err.println ("Not correct");
        if (enemies.contains ("Mike"))
            System.err.println ("Not correct");

// //////////// Uncomment the following when ready for problem 3
    System.out.println ("\n\n Testing problem 3");
    WordList wordList = new WordList();
    List <String> words = wordList.getWordList();
    System.out.println (words.indexOf ("jack"));        // Should be 51595
    if (!words.contains ("zoo"))
        System.err.println ("Error in contains");
    if (words.contains ("foobar"))
        System.err.println ("Error in contains");

    wordList = new WordList();
    List <String> moreWords = wordList.getWordList();
    if (!words.equals (moreWords))
        System.err.println ("Error in equals");
    if (!moreWords.equals (words))
        System.err.println ("Error in equals");
    moreWords.add (0, "foobar");
    if (words.equals (moreWords))
        System.err.println ("Error in equals");
    if (moreWords.equals (words))
        System.err.println ("Error in equals");
    moreWords.remove(0);
    moreWords.add ("foobar");
    if (words.equals (moreWords))
        System.err.println ("Error in equals");
    if (moreWords.equals (words))
        System.err.println ("Error in equals");
    String foobar = new String ("foo" + "bar");
    words.add (foobar);                          // "foobar"
    if (!words.equals (moreWords))
        System.err.println ("Error in equals");
    if (!moreWords.equals (words))
        System.err.println ("Error in equals");
    System.out.println ("Testing complete");
  }
}

節點類

    package list;



public class Node<E> 
{
    E value;
    Node<E> next;
    Node<E> previous;
    Node(E tempValue, Node<E> tempNext, Node<E> tempPrev){
    this.value = tempValue;
    this.next = tempNext;
    this.previous = tempPrev;
    }

}

在不檢查其他代碼的邏輯的情況下,indexOf方法應如下所示:

public int indexOf(Object obj) {
    int index = 0;
    Node<E> current = head;

    while (current != null) {
        if (current.equals(obj)) {
            return index;
        }
        index++;
        current = current.next;
    }

    return -1;
}

基本上,如果您尚未從for循環的主體中返回任何值,則可以推論該列表不包含目標值,並正確返回-1 這就是您需要重寫equals方法的方式

public class Node<E> {
    //...
    public boolean equals(Object o){
           return value.equals(o);
    }
    //...
}

這是可行的,因為<E>在功能上與<E extends Object> 並且,如果您在運行時調用equals整數類型或您指定的類型(它是對象的子類)的任何類型,它將調用駐留在Integer類中的equals方法,或者在繼承繼承時調用最接近的覆蓋版本的equals鏈,而不是Object方法

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM