我是这样写的:
public static void main(String[] args) {
LinkedList<String> list = new LinkedList<>();//declare your list
Scanner input = new Scanner(System.in);//create a scanner
System.out.println("How many participants? ");
int nbr = input.nextInt();//read the number of element
input.nextLine();
do {
System.out.println("What is the name of the people?");
list.add(input.nextLine());//read and insert into your list in one shot
nbr--;//decrement the index
} while (nbr > 0);//repeat until the index will be 0
System.out.println(list);//print your list
输出是:
Jack, Frank
我想做的是改变链表的内容。例如,我写了 2 个名字 Jack 和 Frank。然后我想添加 Jack 的姓氏,这样链表将是:
Jack Adam, Frank
我该怎么办?
最佳答案
您需要做的是使用set
方法更新您要更新的特定位置。在这种情况下将是这样的:
list.set(0, list.get(0) + " Adam");
请注意,我们正在获取列表位置 0 的内容并与姓氏连接。
关于java - 如何更改字符串链表的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50051734/