import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;
public class LinkedListTest {
public static void main(String[] args) {
//添加元素
System.out.println("-------------添加元素---------------");
Float f1 =new Float(120.2f);
LinkedList l1 =new LinkedList();
l1.add(f1);
l1.add(100);
l1.add("柯尼塞格");
l1.add(3.14f);
l1.add(true);
System.out.println(l1.toString());
System.out.println("-------------删除元素---------------");
l1.remove(3);
l1.remove(f1);
l1.remove(new Integer(100));
System.out.println(l1.toString());
System.out.println("-------------遍历元素---------------");
System.out.println("-------------for循环遍历---------------");
for (int i = 0; i < l1.size(); i++) {
System.out.println(l1.get(i));
}
System.out.println("-------------增强for遍历---------------");
for (Object o: l1) {
System.out.println(o.toString());
}
System.out.println("-------------迭代器遍历---------------");
Iterator i1 = l1.iterator();
while (i1.hasNext()){
System.out.println(i1.next());
}
System.out.println("-------------列表迭代器从前往后---------------");
ListIterator listIterator = l1.listIterator();
while(listIterator.hasNext()){
listIterator.next();
}
while (listIterator.hasPrevious()){
System.out.println(listIterator.previous());
}
System.out.println("-------------判断---------------");
System.out.println("是否包含”true“:"+l1.contains(new Boolean(true)));
System.out.println("是否包含”柯尼塞格“:"+l1.contains(new String("柯尼塞格")));
System.out.println("是否包含对象f1:"+l1.contains(f1));
System.out.println("集合是否为空:"+l1.isEmpty());
}
}
输出结果:
-------------添加元素---------------
[120.2, 100, 柯尼塞格, 3.14, true]
-------------删除元素---------------
[柯尼塞格, true]
-------------遍历元素---------------
-------------for循环遍历---------------
柯尼塞格
true
-------------增强for遍历---------------
柯尼塞格
true
-------------迭代器遍历---------------
柯尼塞格
true
-------------列表迭代器从前往后---------------
true
柯尼塞格
-------------判断---------------
是否包含”true“:true
是否包含”柯尼塞格“:true
是否包含对象f1:false
集合是否为空:false