gpt4 book ai didi

java - 包含特定元素的列表

转载 作者:行者123 更新时间:2023-12-02 09:32:31 25 4
gpt4 key购买 nike

我有一个类和一个名为 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"));



}

}

错误消息:

线程“main”中出现异常 java.lang.StackOverflowError 在 arraylist_linkedList.WordList.contains(WordList.java:83)

最佳答案

这里:

public boolean contains(String item) {
if (list.contains(item))

在您的 WordList 实例上,您调用 contains()。该方法...转向它包含自身的one静态WordList,并再次调用contains()。再说一遍,...

您在这里创建了无限递归。如何实现更好的解决方案取决于您到底想要实现什么。最有可能的是,您的起点:

static WordList list = new WordList();

已经是“错误的”。您导入了 ArrayList,那么为什么您的 list 不是 ArrayList 呢?!无论如何,这个列表是静态的根本没有意义,顺便说一句。

因此:您的“每个对象”(非静态)list() 方法在您的类的一个静态列表字段上调用“自身”。这是行不通的,而且从概念上讲也没有意义。

关于java - 包含特定元素的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57835789/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com