linkedlist实现中的tostring()方法存在问题JAVA

mpbci0fu  于 2021-08-25  发布在  Java
关注(0)|答案(1)|浏览(400)

此问题已在此处找到答案

什么是nullpointerexception,如何修复它((12个答案)
三天前关门。
我只是尝试实现一个链表,当我只添加了两个元素时,它抛出了 NullPointerExceptiontoString 方法,但我不知道为什么。当它为空时,它可以打印[],但随后它会抛出一个错误。也许你能帮我找出我的手机出了什么问题 toString() 方法?
如果列表为空,则理想输出为[a、b、c]或[]。
我的全部代码:

  1. import java.util.Iterator;
  2. import java.util.NoSuchElementException;
  3. public class ListImpl implements List {
  4. private Node head;
  5. private Node tail;
  6. private static class Node {
  7. Object data;
  8. Node next;
  9. Node(Object d) {
  10. data = d;
  11. next = null;
  12. }
  13. }
  14. @Override
  15. public void clear() {
  16. if (size() > 0) {
  17. head = null;
  18. tail = null;
  19. }
  20. }
  21. @Override
  22. public int size() {
  23. int size = 0;
  24. Node currentNode = head;
  25. while (currentNode != null) {
  26. size++;
  27. currentNode = currentNode.next;
  28. }
  29. return size;
  30. }
  31. public Iterator<Object> iterator() {
  32. return new IteratorImpl();
  33. }
  34. private class IteratorImpl implements Iterator<Object> {
  35. @Override
  36. public boolean hasNext() {
  37. return head != null;
  38. }
  39. @Override
  40. public Object next() {
  41. if(hasNext()){
  42. Object data = head.data;
  43. head = head.next;
  44. return data;
  45. }
  46. return null;
  47. }
  48. }
  49. @Override
  50. public void addFirst(Object element) {
  51. Node newNode = new Node(element);
  52. newNode.next = null;
  53. if (head == null) {
  54. head = newNode;
  55. }
  56. else {
  57. newNode.next = head;
  58. head = newNode;
  59. }
  60. }
  61. @Override
  62. public void addLast(Object element) {
  63. Node newNode = new Node(element);
  64. newNode.next = null;
  65. if (tail == null) {
  66. tail = newNode;
  67. }
  68. else {
  69. newNode.next = tail;
  70. tail = newNode;
  71. }
  72. }
  73. @Override
  74. public void removeFirst() {
  75. if (head == null) {
  76. System.err.print("The first element is absent");
  77. }
  78. head = head.next;
  79. }
  80. @Override
  81. public void removeLast() {
  82. if (head == null)
  83. System.err.print("Your list is empty");
  84. if (head.next == null) {
  85. System.err.print("There is only one element");
  86. }
  87. // Find the second last node
  88. Node second_last = head;
  89. while (second_last.next.next != null)
  90. second_last = second_last.next;
  91. // Change next of second last
  92. second_last.next = null;
  93. }
  94. @Override
  95. public Object getFirst() {
  96. if(head!=null) {
  97. return head.data;}
  98. else
  99. throw new java.util.NoSuchElementException("List is empty");
  100. }
  101. @Override
  102. public Object getLast() {
  103. if(head == null){
  104. throw new java.util.NoSuchElementException("List is empty");
  105. }
  106. Node last = head;
  107. while (last.next != null)
  108. {
  109. last = last.next;
  110. }
  111. return last;
  112. }
  113. @Override
  114. public Object search(Object element) {
  115. Object result = null;
  116. while (head.next != null)
  117. {
  118. if (head.data.equals(element)){
  119. result = head.data;
  120. }
  121. }
  122. return result;
  123. }
  124. @Override
  125. public boolean remove(Object element) {
  126. boolean isFound = false;
  127. if(head == null) {
  128. throw new NoSuchElementException("List is empty!");
  129. }
  130. if(head.data.equals(element)) {
  131. head = head.next;
  132. return true;
  133. }
  134. Node currentNode = head;
  135. Node previousNode = null;
  136. while(currentNode !=null) {
  137. if(currentNode.data.equals(element)) {
  138. isFound = true;
  139. break;
  140. }
  141. previousNode = currentNode;
  142. currentNode = currentNode.next;
  143. }
  144. if(currentNode == null) {
  145. return isFound;
  146. }
  147. currentNode = previousNode.next;
  148. previousNode.next = currentNode.next;
  149. currentNode.next = null;
  150. return isFound;
  151. }
  152. @Override
  153. public String toString() {
  154. Node current = head;
  155. if(head == null) {
  156. return "[]";
  157. }
  158. StringBuilder result = new StringBuilder();
  159. result.append("[");
  160. while(current.next != null) {
  161. result.append(current.data + ", ");
  162. current = current.next;
  163. }
  164. result.append(tail.data + "]");
  165. return result.toString();
  166. }
  167. public static void main(String[] args) {
  168. ListImpl list = new ListImpl();
  169. list.addFirst("FirstElement");
  170. list.addFirst("SecondElement");
  171. System.out.println(list);
  172. }
  173. }
yvfmudvl

yvfmudvl1#

问题就在眼前

  1. result.append(tail.data + "]");

在方法中不更新尾部 addFirst() 在其他方法中,如果你纠正了这一点,它也会起作用。
请随时对我的回答添加评论,以进行澄清。

相关问题