public class Test12 {
public static void main(String[] args) {
LinkedList<String> list = new LinkedList<>();
list.add("tutu");
list.add("小美");
list.add("uphappy");
System.out.println("list = " + list);
list.add(1,"head");
System.out.println("list = " + list);
list.set(0,"dad");
System.out.println("list = " + list);
System.out.println(list.getFirst());
System.out.println(list.getLast());
//第一种
boolean b = list.isEmpty();
while (!b){
System.out.print(list.pop()+" ");
}
//dad head 小美 uphappy Exception in thread "main" java.util.NoSuchElementException
// at java.util.LinkedList.removeFirst(LinkedList.java:270)
// at java.util.LinkedList.pop(LinkedList.java:801)
// at com.web.Test12.main(Test12.java:23)
//第二种
while(!list.isEmpty()){
System.out.print(list.pop()+" ");
}
//dad head 小美 uphappy
}
}
第一种写法为啥报异常
猜你喜欢
转载自blog.csdn.net/djydjy3333/article/details/121319422
今日推荐
周排行