以下实例演示了使用 listname.add() 和 listname.set() 方法来修改链接中的元素:
/* author by shouce.ren 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); } }
以上代码运行输出结果为:
[B, B, T, H, P] [B, B, M, H, P]