我正在尝试将字符串对象添加到链接列表中,但当我尝试返回列表时不断收到错误
import java.util.LinkedList;
import java.util.ListIterator;
public class StacksAndQueues {
LinkedList<String> list = new LinkedList<String>();
private String[] words = {"goats", "cheese"};
public StacksAndQueues() {}
public String Add() {
/**
This method is used to load an array of string objects
into a linked list
*/
int x;
for (x=0; x < words.length; x++) {
list.add(x, words[x]);
}
return list;
}
}
最佳答案
如果您想从 Add()
方法返回列表,则需要更改该方法的签名。
public LinkedList<String> Add () {
/**
This method is used to load an array of string objects
into a linked list
*/
int x;
for (x=0; x < words.length; x++) {
list.add(x, words[x]);
}
return list;
}
关于java - java中将字符串对象添加到链表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33404997/