Java 链表前置和toString方法

Java 链表前置和toString方法,java,methods,linked-list,Java,Methods,Linked List,我正在尝试输出列表,我在列表前加了3个字符串,所有输出都为空。代码似乎正确地预编和输出,但它只是不输出数据。我关注的方法是prepend和toString `` 公共类字符串列表{ /* ------------------------------------------------------- * * Inner classes * * ----------------------------------

我正在尝试输出列表,我在列表前加了3个字符串,所有输出都为空。代码似乎正确地预编和输出,但它只是不输出数据。我关注的方法是prepend和toString

`` 公共类字符串列表{

/* ------------------------------------------------------- *
 * Inner classes                                           *
 * ------------------------------------------------------- */
/**
 * The node used by StringList.
 */
private class StrNode {

    String data;
    StrNode next;
}

/* ------------------------------------------------------- *
 * Instance variables                                      *
 * ------------------------------------------------------- */
private StrNode head;   // the head of the singly-linked list.

/* ------------------------------------------------------- *
 * Instance methods                                        *
 * ------------------------------------------------------- */
/**
 * No-argument constructor.
 */
public StringList() {
    head = null;
}

/**
 * Adds an item to the start of the list.
 *
 * @param s the item to add
 */
public void prepend(String s) {
    var newNode = new StrNode();
    // TODO: Adds an item to the start of the list.     
    if(head == null) {
        head = newNode;
    }
    else {
    newNode.next = head.next;
    head.next = newNode;
    }
    
}

/**
 * Adds an item to the end of the list.
 *
 * @param s the item to add
 */
public void append(String s) {
    // TODO:    Adds an item to the end of the list.
    
}

/**
 * Inserts an item after the first instance of a key if the key exists.
 *
 * @param s the item to insert
 * @param key the item in the list to insert after
 * @return whether the insertion was successful
 */
public boolean insertAfter(String s, String key) {
    // TODO:    Inserts an item after the first instance of a key if the key exists.
    
    return false;
}

/**
 * Deletes the first instance of an item from the list.
 *
 * @param key the value of the item to delete from the list.
 * @return whether the deletion was successful.
 */
public boolean delete(String key) {
    // TODO:    Deletes the first instance of an item from the list.
    
    return false;
}

/**
 * Returns the value of the nth item in the list.
 * 
 * @param n the zero-based index of the item to return
 * @return the value of the nth item
 */
public String get(int n) {
    // TODO:    Returns the value of the nth item in the list.
    // Note: if n is out of bounds, raise an IndexOutOfBoundsException.
    
    
    return null;
}

/**
 * Returns the number of items in the list.
 *
 * @return the number of items in the list
 */
public int length() {
    // TODO:    Returns the number of items in the list.
    int length = 0;
    StrNode current = head;
    
    while(current != null) {
        length++;
        current = current.next;
    }
    
    return length;
}

/**
 * Returns a string of all the items in the list separated by a space.
 *
 * The last item will have a space after it too.
 *
 * @return list of the list's values
 */
@Override
public String toString() {
    // TODO:    Returns a string of all the items in the list separated by a space.
    String result = "{";
    StrNode current = this.head;
    
    while(current != null) {
        result += current.data + " ";
        current = current.next;
    }
 
    return result + "}";

}
} ``

司机: ``

公共班机{ /** *@param指定命令行参数 */ 公共静态void main(字符串[]args){

}


``

错误在您的
前缀中(字符串s)
method。仔细查看-您从未使用过
s
,也就是说,您传递给此方法的字符串从未被存储。您只需添加
newNode.data=s;
作为第二行。但是,由于
StrNode
对象链接不正确,因此还有第二个错误

解决方案

public void prepend(String s) {
    var newNode = new StrNode();
    newNode.data = s;
    if (head == null) {
        head = newNode;
    } else {
        // First, we set the current head as the successor of the new newNode
        newNode.next = head;
        // Then, we set the new newNode as head (as we prepend)
        head = newNode;
    }
}
然后输出如下所示:

{three two one }
附加评论

您的
toString()
方法将字符串与
+
合并在一个循环中。该方法效率低下,应使用
StringBuilder
替换。StringBuilder还允许轻松删除“一”之后的最后一个空格:

{three two one }
@Override
public String toString() {
    StringBuilder result = new StringBuilder("{");
    StrNode current = this.head;

    while (current != null) {
        result.append(current.data).append(" ");
        current = current.next;
    }
    result.setLength(result.length() - 1);

    return result + "}";
}