JAVA LinkedList小例

package com.test.collframework;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;

public class LinkedListTest {

	public static void main(String[] args) {
		LinkedList<String> aList = new LinkedList<String>();
		aList.add("apple");
		aList.add("pear");
		aList.add("banana");
		
		LinkedList<String> bList = new LinkedList<String>();
		bList.add("cat");
		bList.add("dog");
		bList.add("bird");
		
		Iterator ait = aList.iterator();
		//ListIterator(列表迭代器)有add及previous方法
		ListIterator bit = bList.listIterator();
		while(ait.hasNext()){ //隔个插入
			if(bit.hasNext()){
				bit.next();
			}
			bit.add(ait.next());
		}
		
		ait = aList.iterator();
		while(ait.hasNext()){
			ait.next();
			if(ait.hasNext()){ //只保留最后一个,前面的都删除
				ait.remove();
			}
		}
		System.out.println(aList);
		System.out.println(bList);
		bList.removeAll(aList);
		System.out.println(bList);
	}

}
 

打印结果

 

[banana]

[cat, apple, dog, pear, bird, banana]

[cat, apple, dog, pear, bird]

你可能感兴趣的:(LinkedList)