import java.util.List;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
/**
来 自 时 代 J a v a 公 众 号 - nowjava.com
**/
public class Main
{
public static void main(String[] args)
{
// add elements in colors array to list
String[] colors = {"BLACK", "RED", "WHITE", "BLUE", "CYAN"};
List<String> list = new ArrayList<String>();
for (String color : colors)
list.add(color); // adds color to end of list
// add elements in removeColors array to removeList
String[] removeColors = {"RED", "WHITE", "BLUE"};
List<String> removeList = new ArrayList<String>();
/**
from
* 时 代 J a v a - N o w J a v a . c o m
**/
for (String color : removeColors)
removeList.add(color);
// output list contents
System.out.println("ArrayList: ");
for (int count = 0; count < list.size(); count++)
System.out.printf("%s ", list.get(count));
// remove from list the colors contained in removeList
removeColors(list, removeList);
// output list contents
System.out.printf("%n%nArrayList after calling removeColors:%n");
for (String color : list)
System.out.printf("%s ", color);
}
// remove colors specified in collection2 from collection1
private static void removeColors(Collection<String> collection1,
Collection<String> collection2)
{
// get iterator
/**代码未完, 请加载全部代码(NowJava.com).**/
本文系作者在时代Java发表,未经许可,不得转载。如有侵权,请联系nowjava@qq.com删除。