Java 如何使用myStringList插入最后一个元素
我对myStringList.insertLast(name) 我不确定是否需要将Java 如何使用myStringList插入最后一个元素,java,Java,我对myStringList.insertLast(name) 我不确定是否需要将.insertLast更改为其他内容。错误表示无法将名称解析为变量 public class TestLinkedList { public static void main(String [] args) { // define an Integer linked list MyIntList LinkedListClass<Integer> MyIntegerList = ne
.insertLast
更改为其他内容。错误表示无法将名称解析为变量
public class TestLinkedList
{
public static void main(String [] args)
{
// define an Integer linked list MyIntList
LinkedListClass<Integer> MyIntegerList = new LinkedListClass<Integer>();
// intitialize the list
MyIntegerList.initializeList();
// insert a last element 5, then 6, and 7
MyIntegerList.insertLast(5);
MyIntegerList.insertLast(6);
MyIntegerList.insertLast(7);
// print the list
MyIntegerList.print();
// insert first element 8
MyIntegerList.insertFirst(11);
// print the list
MyIntegerList.print();
// define a String linked list MyStringList
LinkedListClass<String> MyStringList = new LinkedListClass<String>();
// initialize the list
MyStringList.initializeList();
// insert last element Chris ,then Tom, and Bo
MyStringList.insertLast(Chris);
MyStringList.insertLast(Tom);
MyStringList.insertLast(Bo);
// print the list
MyStringList.print();
// insert first element Bo
MyStringList.insertFirst(Anna);
// print the list
MyStringList.print();
}
}
字符串必须用双引号(“)括起来。编译问题的解决方案如下:
MyStringList.insertLast("Chris");
MyStringList.insertLast("Tom");
MyStringList.insertLast("Bo");
...
// insert first element Bo
MyStringList.insertFirst("Anna");
名称是字符串,在java中必须使用“”
java中的字符串文字必须包含在“字符串”中,否则将被视为变量。“克里斯”,“汤姆”和“波”。啊,我早该知道的。谢谢@arynaq
MyStringList.insertLast("Chris");
MyStringList.insertLast("Tom");
MyStringList.insertLast("Bo");
...
// insert first element Bo
MyStringList.insertFirst("Anna");
MyStringList.insertLast("Chris");
MyStringList.insertLast("Tom");
MyStringList.insertLast("Bo");
MyStringList.insertFirst("Anna");