递归 - 在列表的前面和末尾插入值

时间:2017-11-29 15:09:05

标签: java if-statement recursion junit

我在使用这种方法时遇到了一些麻烦。它应该在给定的索引处插入一个值。

    public Element insertElementAt(int value, int index) {

    if (index == 0 || this.next == null) { 
        Element newElement = new Element();
        System.out.println("you are in the loop");
        newElement.setNext(this);
        System.out.println("you are still in the loop");
        return this;
    }
else if (this.next !=null) {
        index--;
        this.next = this.next.insertElementAt(value, index);
        return this;
    }
    if (this.next== null){
        return this; 
    }
    return this;
}

这个想法是,该方法应该能够在我的元素的前面,中间和末尾插入一个值。我仍然是递归的新手,但已经设法走到这一步 - 也是为什么我添加了System.out.println。
 我很确定,我只需要更改/添加/删除一行或两行,但我无法弄清楚在哪里。我也相信它是在if语句中。

以防万一的测试用例:

  @Test
public void testInsertElementAt_Front() {
    Element el = createElements(0, 1, 2);
    Element result = el.insertElementAt(11, 0);
    System.out.println(result.showValues());
    assertEquals(11, result.getValue());
    assertEquals(0, result.getNext().getValue());
}

@Test
public void testInsertElementAt_Middle() {
    Element el = createElements(0, 1, 2);
    Element result = el.insertElementAt(11, 1);
    System.out.println(result.showValues());
    assertEquals(0, result.getValue());
    assertEquals(11, result.getNext().getValue());
}

@Test
public void testInsertElementAt_End() {
    Element el = createElements(0, 1);
    System.out.println(el.showValues()); 
    Element result = el.insertElementAt(11, 2);
    System.out.println(result.showValues()); 
    assertEquals(0, result.getValue());
    assertEquals(1, result.getNext().getValue());
    assertEquals(11, result.getNext().getNext().getValue());
    assertNull(result.getNext().getNext().getNext());
}

输出:

testInsertElementAt_Front()
junit.framework.AssertionFailedError: expected:<11> but was:<0>

testInsertElementAt_Middle()
 junit.framework.AssertionFailedError: expected:<11> but was:<1>

testInsertElementAt_Endjava.lang.NullPointerException

任何帮助将不胜感激

代码的格式有点(else if),它在复制代码时发生,这不是问题

这是Element类:

public class Element {

private int value;
private Element next;

/**
 *
 * @return
 */
public int getValue() {
    return value;
}

/**
 *
 * @param value
 */
public void setValue(int value) {
    this.value = value;
}

/**
 *
 * @param next
 */
public void setNext(Element next) {
    this.next = next;
}

public Element getNext() {
    return next;
}

/**
 *
 * @param value
 * @return
 */
public Element appendElement(int value) {
    if (this.next == null) {
        Element newElement = new Element();
        newElement.setValue(value);
        this.next = newElement;
    } else {
        this.next = this.next.appendElement(value);
    }
    return this;
}

/**
 *
 * @param value
 * @return
 */
public Element insertElementSorted(int value) {
    if (this.value > value) {
        Element newElement = new Element();
        newElement.setValue(value);
        newElement.setNext(this);
        return newElement;
    } else if (this.next == null) {
        Element newElement = new Element();
        newElement.setValue(value);
        this.next = newElement;
        return this;
    } else {
        this.next = this.next.insertElementSorted(value);
        return this;
    }
}

/**
 *
 * @param value
 * @return
 */
public Element deleteElement(int value) {
    if (this.value == value) {
        return this.next;
    } else {
        if (this.next != null) {
            this.next = this.next.deleteElement(value);
        }
        return this;
    }
}

/**
 *
 * @return
 */
public int size() {
    if (this.next == null) {
        return 1;
    } else {
        return 1 + this.next.size();
    }
}

/**
 *
 * @return
 */
public int sum() {
    if (this.next == null) {
        return this.value;
    } else {
        return value + this.next.sum();
    }
}

/**
 *
 * @return
 */
public boolean isSorted() {
    if (this.next == null) {
        return true;
    } else if (this.value > this.next.value) {
        return false;
    } else {
        return this.next.isSorted();
    }
}

/**
 *
 * @param value
 * @return
 */
public boolean existsElement(int value) {
    if (this.value == value) {
        return true;
    } else if (this.next == null) {
        return false;
    } else {
        return this.next.existsElement(value);
    }
}

/**
 *
 * @return
 */
public String showValues() {
    if (this.next == null) {
        return this.value + "";
    } else {
        return this.value + " " + this.next.showValues();
    }
}

/**
 *
 * @param index
 * @return
 */
public int getValueAt(int index) {
    if (index == 0) {
        return this.value;
    } else if (this.next == null || index < 0) {
        return Integer.MAX_VALUE;
    } else {
        index--;
        return this.next.getValueAt(index);
    }

}

/**
 *
 * @param value
 * @param index
 * @return this
 */
public Element insertElementAt(int value, int index) {
    if (index == 0 || this.next == null) {
        Element newElement = new Element();
        newElement.setNext(this);
        return newElement;
    }
    else if (this.next != null) {
        index--;
        this.next = this.next.insertElementAt(value, index);
        return this;
    }
    if (this.next == null) {
        return this;
    }
    return this;
}

/**
 *
 * @param value
 * @return element
 */
public Element insertElementAtFront(int value) {
    Element element = new Element();
    element.setValue(value);
    element.setNext(this);

    return element;
}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
}

}

0 个答案:

没有答案