为什么我的ToString()方法不以字符串格式返回我的单独链接列表?
我正在尝试实现一种toString()方法,该方法通过遍历列表,以字符串格式返回我的单连锁列表中的值。请参阅下面的SINGLELINKEDLIST.JAVA文件,其中包含toString()方法。
import java.util.NoSuchElementException;
import java.util.*;
/**
* Your implementation of a Singly-Linked List.
*/
public class SinglyLinkedList<T> {
/*
* Do not add new instance variables or modify existing ones.
*/
private SinglyLinkedListNode<T> head;
private SinglyLinkedListNode<T> tail;
private int size;
/*
* Do not add a constructor.
*/
/**
* Adds the element to the front of the list.
*
* Method should run in O(1) time.
*
* @param data the data to add to the front of the list
* @throws java.lang.IllegalArgumentException if data is null
*/
public void addToFront(T data) {
if (data == null){
throw new IllegalArgumentException("Data cannot be null");
}
SinglyLinkedListNode<T> newNode = new SinglyLinkedListNode<T>(data);
if (head == null){
head = newNode;
tail = newNode;
}
else{
newNode.setNext(head);
head = newNode;
}
size++;
}
/**
* Adds the element to the back of the list.
*
* Method should run in O(1) time.
*
* @param data the data to add to the back of the list
* @throws java.lang.IllegalArgumentException if data is null
*/
public void addToBack(T data) {
if (data == null){
throw new IllegalArgumentException("Data cannot be null");
}
if (head == null){
head = new SinglyLinkedListNode<T>(data);
}
else {
SinglyLinkedListNode<T> current = head;
while (current.getNext() != null){
current = current.getNext();
}
current.setNext(new SinglyLinkedListNode<T>(data));
}
size++;
}
/**
* Removes and returns the first data of the list.
*
* Method should run in O(1) time.
*
* @return the data formerly located at the front of the list
* @throws java.util.NoSuchElementException if the list is empty
*/
public T removeFromFront() {
if (head == null){
throw new NoSuchElementException("You cannot remove elements from the front of an empty list");
}
SinglyLinkedListNode<T> var = head;
head = head.getNext();
size--;
return var.getData();
}
/**
* Removes and returns the last data of the list.
*
* Method should run in O(n) time.
*
* @return the data formerly located at the back of the list
* @throws java.util.NoSuchElementException if the list is empty
*/
public T removeFromBack() {
if (head == null){
throw new NoSuchElementException("You cannot remove an element from the back of an empty list");
}
else if (head.getNext() == null){
SinglyLinkedListNode<T> var = head;
head = null;
}
SinglyLinkedListNode<T> current = head;
while (current.getNext().getNext() != null){
current = current.getNext();
}
SinglyLinkedListNode<T> var = current.getNext();
current.setNext(null);
size--;
return var.getData();
}
/**
* Returns the head node of the list.
*
* For grading purposes only. You shouldn't need to use this method since
* you have direct access to the variable.
*
* @return the node at the head of the list
*/
public SinglyLinkedListNode<T> getHead() {
// DO NOT MODIFY THIS METHOD!
return head;
}
/**
* Returns the tail node of the list.
*
* For grading purposes only. You shouldn't need to use this method since
* you have direct access to the variable.
*
* @return the node at the tail of the list
*/
public SinglyLinkedListNode<T> getTail() {
// DO NOT MODIFY THIS METHOD!
return tail;
}
/**
* Returns the size of the list.
*
* For grading purposes only. You shouldn't need to use this method since
* you have direct access to the variable.
*
* @return the size of the list
*/
public int size() {
// DO NOT MODIFY THIS METHOD!
return size;
}
public String toString() {
String answer = "";
SinglyLinkedListNode<T> current = head;
while (current != null){
answer += current + " ";
current = current.getNext();
}
return answer;
}
public static void main(String[] args){
SinglyLinkedList<Integer> list = new SinglyLinkedList<>();
list.addToFront(1);
System.out.println(list.toString());
}
}
对于我的singlylinkedlistnode.java文件...
/**
* Node class used for implementing the SinglyLinkedList.
*
* DO NOT MODIFY THIS FILE!!
*
* @author CS 1332 TAs
* @version 1.0
*/
public class SinglyLinkedListNode<T> {
private T data;
private SinglyLinkedListNode<T> next;
/**
* Constructs a new SinglyLinkedListNode with the given data and next node
* reference.
*
* @param data the data stored in the new node
* @param next the next node in the list
*/
SinglyLinkedListNode(T data, SinglyLinkedListNode<T> next) {
this.data = data;
this.next = next;
}
/**
* Creates a new SinglyLinkedListNode with only the given data.
*
* @param data the data stored in the new node
*/
SinglyLinkedListNode(T data) {
this(data, null);
}
/**
* Gets the data.
*
* @return the data
*/
T getData() {
return data;
}
/**
* Gets the next node.
*
* @return the next node
*/
SinglyLinkedListNode<T> getNext() {
return next;
}
/**
* Sets the next node.
*
* @param next the new next node
*/
void setNext(SinglyLinkedListNode<T> next) {
this.next = next;
}
}
每当我从单身链接类拨打toString()方法时,我都会在命令提示符中获得以下打印。
有人知道为什么我不看到我添加到的值“ 1”在调用ToString()方法之前,LinkedList的正面?如果是这样,我该怎么办才能解决?相反,它继续打印节点对象。
I am trying to implement a toString() method that returns the values in my singly-linked list in a string format by iterating through the list. See below for the SinglyLinkedList.java file which contains the toString() method.
import java.util.NoSuchElementException;
import java.util.*;
/**
* Your implementation of a Singly-Linked List.
*/
public class SinglyLinkedList<T> {
/*
* Do not add new instance variables or modify existing ones.
*/
private SinglyLinkedListNode<T> head;
private SinglyLinkedListNode<T> tail;
private int size;
/*
* Do not add a constructor.
*/
/**
* Adds the element to the front of the list.
*
* Method should run in O(1) time.
*
* @param data the data to add to the front of the list
* @throws java.lang.IllegalArgumentException if data is null
*/
public void addToFront(T data) {
if (data == null){
throw new IllegalArgumentException("Data cannot be null");
}
SinglyLinkedListNode<T> newNode = new SinglyLinkedListNode<T>(data);
if (head == null){
head = newNode;
tail = newNode;
}
else{
newNode.setNext(head);
head = newNode;
}
size++;
}
/**
* Adds the element to the back of the list.
*
* Method should run in O(1) time.
*
* @param data the data to add to the back of the list
* @throws java.lang.IllegalArgumentException if data is null
*/
public void addToBack(T data) {
if (data == null){
throw new IllegalArgumentException("Data cannot be null");
}
if (head == null){
head = new SinglyLinkedListNode<T>(data);
}
else {
SinglyLinkedListNode<T> current = head;
while (current.getNext() != null){
current = current.getNext();
}
current.setNext(new SinglyLinkedListNode<T>(data));
}
size++;
}
/**
* Removes and returns the first data of the list.
*
* Method should run in O(1) time.
*
* @return the data formerly located at the front of the list
* @throws java.util.NoSuchElementException if the list is empty
*/
public T removeFromFront() {
if (head == null){
throw new NoSuchElementException("You cannot remove elements from the front of an empty list");
}
SinglyLinkedListNode<T> var = head;
head = head.getNext();
size--;
return var.getData();
}
/**
* Removes and returns the last data of the list.
*
* Method should run in O(n) time.
*
* @return the data formerly located at the back of the list
* @throws java.util.NoSuchElementException if the list is empty
*/
public T removeFromBack() {
if (head == null){
throw new NoSuchElementException("You cannot remove an element from the back of an empty list");
}
else if (head.getNext() == null){
SinglyLinkedListNode<T> var = head;
head = null;
}
SinglyLinkedListNode<T> current = head;
while (current.getNext().getNext() != null){
current = current.getNext();
}
SinglyLinkedListNode<T> var = current.getNext();
current.setNext(null);
size--;
return var.getData();
}
/**
* Returns the head node of the list.
*
* For grading purposes only. You shouldn't need to use this method since
* you have direct access to the variable.
*
* @return the node at the head of the list
*/
public SinglyLinkedListNode<T> getHead() {
// DO NOT MODIFY THIS METHOD!
return head;
}
/**
* Returns the tail node of the list.
*
* For grading purposes only. You shouldn't need to use this method since
* you have direct access to the variable.
*
* @return the node at the tail of the list
*/
public SinglyLinkedListNode<T> getTail() {
// DO NOT MODIFY THIS METHOD!
return tail;
}
/**
* Returns the size of the list.
*
* For grading purposes only. You shouldn't need to use this method since
* you have direct access to the variable.
*
* @return the size of the list
*/
public int size() {
// DO NOT MODIFY THIS METHOD!
return size;
}
public String toString() {
String answer = "";
SinglyLinkedListNode<T> current = head;
while (current != null){
answer += current + " ";
current = current.getNext();
}
return answer;
}
public static void main(String[] args){
SinglyLinkedList<Integer> list = new SinglyLinkedList<>();
list.addToFront(1);
System.out.println(list.toString());
}
}
And for my SinglyLinkedListNode.java file...
/**
* Node class used for implementing the SinglyLinkedList.
*
* DO NOT MODIFY THIS FILE!!
*
* @author CS 1332 TAs
* @version 1.0
*/
public class SinglyLinkedListNode<T> {
private T data;
private SinglyLinkedListNode<T> next;
/**
* Constructs a new SinglyLinkedListNode with the given data and next node
* reference.
*
* @param data the data stored in the new node
* @param next the next node in the list
*/
SinglyLinkedListNode(T data, SinglyLinkedListNode<T> next) {
this.data = data;
this.next = next;
}
/**
* Creates a new SinglyLinkedListNode with only the given data.
*
* @param data the data stored in the new node
*/
SinglyLinkedListNode(T data) {
this(data, null);
}
/**
* Gets the data.
*
* @return the data
*/
T getData() {
return data;
}
/**
* Gets the next node.
*
* @return the next node
*/
SinglyLinkedListNode<T> getNext() {
return next;
}
/**
* Sets the next node.
*
* @param next the new next node
*/
void setNext(SinglyLinkedListNode<T> next) {
this.next = next;
}
}
Whenever I call the toString() method from my SinglyLinkedList class, I get the following printed in my command prompt.
Does anyone know why I do not see the value "1" which I am adding to the front of the linkedlist prior to calling the toString() method? And if so, what can I do to fix it? Instead, it keeps printing the node object.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题在于您的对象“当前”的singlylinkedlist的对象没有被覆盖。
如果您不能在单链接ListlistNode类中覆盖ToString方法,则可以将WAIL循环修改为:
假设您的T具有适当的ToString()方法。
为了提高代码效率,您可以使用StringBuilder而不是串联字符串。
}
The problem is you have an object 'current' of type SinglyLinkedList whose toString method is not overridden.
If you cannot override toString method in SinglyLinkedListNode class then you can modify the while loop to:
Assuming your T has appropriate toString() method overridden.
To make the code more efficient you can use StringBuilder rather than concatenating strings.
}