Java源码——从一个列表中移除一个子列表 (remove elements contained in ArrayList B from ArrayList A)

源码如下:

 

 

//Fig. 16.2: CollectionTest.java
//Collection interface demonstrated via an ArrayList object.
import java.util.List;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class CollectionTest 
{
public static void main(String[] args)
{
   // add elements in colors array to list
   String[] colors = {"MAGENTA", "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>();

   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
   Iterator<String> iterator = collection1.iterator(); 

   // loop while collection has items
   while (iterator.hasNext())         
   {
      if (collection2.contains(iterator.next()))
         iterator.remove(); // remove current element
   } 
} 
} // end class CollectionTest

程序解读:

16.6.1 ArrayList and Iterator  Figure 16.2 uses an ArrayList (introduced in Section 7.16) to demonstrate several capabilities  of interface Collection. The program places two Color arrays in ArrayLists and  uses an Iterator to remove elements in the second ArrayList collection from the first. Lines 13 and 20 declare and initialize String arrays colors and removeColors. Lines  14 and 21 create ArrayList<String> objects and assign their references to List<String>  variables list and removeList, respectively. Recall that ArrayList is a generic class, so we  can specify a type argument (String in this case) to indicate the type of the elements in each  list. Because you specify the type to store in a collection at compile time, generic collections  provide compile-time type safety that allows the compiler to catch attempts to use  invalid types. For example, you cannot store Employees in a collection of Strings.  Lines 16–17 populate list with Strings stored in array colors, and lines 23–24 populate  removeList with Strings stored in array removeColors using List method add.  Lines 29–30 output each element of list. Line 29 calls List method size to get the  number of elements in the ArrayList. Line 30 uses List method get to retrieve indi-vidual element values. Lines 29–30 also could have used the enhanced for statement  (which we’ll demonstrate with collections in other examples).  Line 33 calls method removeColors (lines 43–55), passing list and removeList as  arguments. Method removeColors deletes the Strings in removeList from the Strings  in list. Lines 38–39 print list’s elements after removeColors completes its task.  Method removeColors declares two Collection<String> parameters (lines 43–  44)—any two Collections containing Strings can be passed as arguments. The method  accesses the elements of the first Collection (collection1) via an Iterator. Line 47 calls  Collection method iterator to get an Iterator for the Collection. Interfaces Collection  and Iterator are generic types. The loop-continuation condition (line 50) calls  Iterator method hasNext to determine whether there are more elements to iterate  through. Method hasNext returns true if another element exists and false otherwise.  The if condition in line 52 calls Iterator method next to obtain a reference to the  next element, then uses method contains of the second Collection (collection2) to  determine whether collection2 contains the element returned by next. If so, line 53 calls  Iterator method remove to remove the element from the Collection collection1.

 

--------------------------------------------------------------------------------------------------------------------------------------------------------------

1. Iterator<String> java.util.Collection.iterator()

  • iterator

    Iterator<E> iterator()

    Returns an iterator over the elements in this collection. There are no guarantees concerning the order in which the elements are returned (unless this collection is an instance of some class that provides a guarantee).

    Specified by:

    iterator in interface Iterable<E>

    Returns:

    an Iterator over the elements in this collection

2. boolean java.util.Iterator.hasNext()

 

 

  • hasNext

    boolean hasNext()

    Returns true if the iteration has more elements. (In other words, returns true if next() would return an element rather than throwing an exception.)

    Returns:

    true if the iteration has more elements

3.

java.util.ArrayList.ArrayList<String>()

 

 

  • ArrayList

    public ArrayList()

    Constructs an empty list with an initial capacity of ten.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值