package ceshi;
import java.util.Iterator;import java.util.LinkedList;public class LinkedListDemo { public static void main(String[] args) { // TODO 自动生成的方法存根 fun2(); } public static void fun() { LinkedList<String> link = new LinkedList<String>(); link.addLast("张无忌1"); link.addLast("张无忌2"); link.addLast("张无忌3"); link.addFirst("1"); link.addFirst("2"); link.addFirst("3"); Iterator it = link.iterator(); while (it.hasNext()) { String type = (String) it.next(); System.out.println(type); } }getFirst(); getLast(); 先判断集合是否为空后 在进行循环或者获得操作
public static void fun2(){ LinkedList<String> link = new LinkedList<String>(); link.add("杜甫"); link.add("杜甫1"); link.add("杜甫2"); link.add("杜甫3"); String first2 = link.getFirst(); System.out.println(first2); if(!link.isEmpty()) { String first = link.getFirst(); System.out.println(first); } }//删除功能
public static void fun2() { LinkedList<String> link = new LinkedList<String>(); link.add("每当我听到雨声"); link.add("就像听到了心中的迷茫"); link.removeFirst(); System.out.println(link); } }