package com.msb.test03; import java.util.Iterator; import java.util.LinkedList; /** * @author : liu * 日期:16:29:54 * 描述:IntelliJ IDEA * 版本:1.0 */ public class Test { //这是一个main方法:是程序的入口 public static void main(String[] args) { /* LinkedList常用方法: 增加:addFirst(E e) addLast(E e) offer(E e) offerFirst(E e) offerLast(E e) 删除:poll() pollFirst() pollLast() -->jdk1.6以后新出的方法,提高了代码的健壮性 removeFirst() removeLast() 修改: 查看:element() getFirst() getLast() getLast() lastIndexOf(Object o) peek() peekFirst() peekLast() 判断: * */ //创建一个LinkedList的对象 LinkedList<String> list=new LinkedList<>(); list.add("aaaaa"); list.add("bbbbb"); list.add("ccccc"); list.add("ddddd"); list.add("eeeee"); list.add("bbbbb"); list.add("fffff"); list.addFirst("jj"); list.addLast("hh"); list.offer("kk");//添加数据在尾端 list.offerFirst("pp"); list.offerLast("rr"); list.poll();//删除头部元素,并且将元素输出 System.out.println(list.pollFirst()); System.out.println(list.pollLast()); System.out.println(list.removeFirst()); System.out.println(list.removeLast()); System.out.println(list);//LinkedList可以添加重复数据 //list.clear();//清空集合 System.out.println(list); //System.out.println(list.pollFirst()); //list.removeFirst();//Exception in thread "main" java.util.NoSuchElementException //at java.util.LinkedList.removeFirst(LinkedList.java:270) //at com.msb.test03.Test.main(Test.java:50) //集合的遍历: System.out.println("============================"); //普通for循环 for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } System.out.println("==========================="); //增强for循环 for (String s:list) { System.out.println(s); } //迭代器 System.out.println("==========================="); Iterator<String> iterator = list.iterator(); while(iterator.hasNext()){ System.out.println(iterator.next()); } //迭代器 //下面这种方式好,节省了内存 for (Iterator<String> it = list.iterator();it.hasNext();){ System.out.println(it.next()); } } }
标签:LinkedList,实现,list,System,add,println,out From: https://www.cnblogs.com/jeldp/p/16841665.html