Java 实例 - 链表修改
以下实例演示了使用 listname.add() 和 listname.set() 方法来修改链接中的元素:
Main.java 文件
import java.util.LinkedList;
public class Main {
public static void main(String[] a) {
LinkedList officers = new LinkedList();
officers.add("B");
officers.add("B");
officers.add("T");
officers.add("H");
officers.add("P");
System.out.println(officers);
officers.set(2, "M");
System.out.println(officers);
}
}
import java.util.LinkedList;
public class Main {
public static void main(String[] a) {
LinkedList officers = new LinkedList();
officers.add("B");
officers.add("B");
officers.add("T");
officers.add("H");
officers.add("P");
System.out.println(officers);
officers.set(2, "M");
System.out.println(officers);
}
}
以上代码运行输出结果为:
[B, B, T, H, P]
[B, B, M, H, P]