我有一个类和一个名为contains的方法-检查列表是否包含给定的项目。我收到一个StackOverflow错误,想知道为什么。
我创建了列表,并在列表中添加了5个字符串。我正在检查字符串元素之一是否在列表中(如果是,则为true,否则为false)
package arraylist_linkedList;
import java.util.ArrayList;
import java.util.Arrays;
/**
* WordList is a singly linked list of Strings. It is designed to demonstrate
* how linked structures work.
*
* @author ..........
*/
public class WordList {
private Node head;
private Node tail;
private int n; // number of words in the list
static WordList list = new WordList();
/**
* Node of LinkedList that stores the item and a single reference to the next
* node.
*/
private class Node {
private String item;
private Node next;
}
/**
* Adds a node containing the new item at the end of the list.
*
* @param newItem
*/
public void append(String newItem) {
// create a new node based on the word provided by the user
Node newNode = new Node();
newNode.item = newItem;
if (isEmpty()) {
head = newNode;
tail = newNode;
} else {
tail.next = newNode;
tail = newNode;
}
n++;
}
/**
* Adds a node containing the new item at the front of the list.
*
* @param newItem
*/
public void prepend(String newItem) {
Node newNode = new Node();
newNode.item = newItem;
newNode.next = head;
head = newNode;
n++;
if (tail == null) {
tail = head;
}
n++;
}
/**
* Returns the index of the first occurrence of the specified item. If the
* specified item in not part of the list the method indexOf returns -1
*
* @param item
* @return index of the first occurrence of the item; -1 if the word was not
* found.
*/
public int indexOf(String item) {
return -1; // TODO 3
}
/**
* Checks whether the list contains the given item.
*
* @param item
* @return true if the item is contained in the list; false otherwise.
*/
public boolean contains(String item) {
if (list.contains(item)) {
return true;
}
return false; // TODO
}
/**
* Returns the number of elements in the list
*
* @return the number of elements
*/
public int size() {
return n;
}
/**
* Determines whether the list is empty or not.
*
* @return true if there are no elements in the list.
*/
public boolean isEmpty() {
return n == 0;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
Node current = head;
while (current != null) {
sb.append(current.item).append(" ");
current = current.next;
}
return sb.toString();
}
/* * * * * * * * Test Client * * * * * * */
public static void main(String[] args) {
//print the list
System.out.println("size: " + list.size());
// print The list is empty. or The list is not empty.
// use a ternary operator to check whether the list is empty
System.out.println(list.isEmpty() ? "List is empty" : "List is not empty" );
// add words to list
list.append("ant");
list.append("bat");
list.append("cow");
list.append("dog");
//print list
System.out.println("list: " + list);
//add new item at the front of the list
list.prepend("mouse");
System.out.println("list: " + list);
System.out.println("list contains: " + list.contains("ant"));
}
}
错误消息:
线程“主”中的异常java.lang.StackOverflowError 在arraylist_linkedList.WordList.contains(WordList.java:83)
答案 0 :(得分:1)
这里:
public boolean contains(String item) {
if (list.contains(item))
在您的WordList
实例上,您调用contains()
。该方法...将转换为包含其自身的 one 静态WordList
,然后再次调用contains()
。再次,...
您在这里创建了无尽的递归。如何实施更好的解决方案取决于您要实现的目标。您最有可能的起点:
static WordList list = new WordList();
已经“错误”。您导入了ArrayList,所以为什么您的list
不是ArrayList
?无论如何,这个列表是 static ,顺便说一句。
因此:您的“每个对象”(不是静态的)list()
方法在类的那个 static 列表字段上调用“自身”。那行不通,从概念上讲也没有意义。