『java.lang.util』-- collection 接口研究

   该接口下面的 removeall(Collection<> c ) 方法的目标效果是移除除了不在集合c里面的所有元素,

   在collection接口继承了Iterable<> E 类,

   collection接口没有实现任何方法  , 里面的方法有 

 1.  int size();  If this collection  contains more than <tt>Integer.MAX_VALUE</tt> elements,  returns <tt>Integer.MAX_VALUE</tt>. //研究一下Integer.MAX_VALUE是什么东西

                                                                                            

 2.  boolean isEmpty();

                                                                                            

 3.  boolean contains(Object o); 判断o在不在集合里面 

                                                                                           

  4.  boolean add(E e);   源码的注释很有意思,‘<tt>true</tt> if this collection changed as a result of the   call’   如果集合因为这个方法的调用而改变了,就返回true。原来是通过判断集合是否改变了,来判断是否添加成功。

                                                                                            

 5.  boolean addAll(Collection<? extends E> c); Adds all of the elements in the specified collection to this collection 添加c里面所有的元素到collection里面,注意:The behavior of this operation is undefined if the specified collection is modified while the operation is in progress.,这个操作是不确定的 ,因为c可能在add操作的过程当中被改变。

                                                                                            

 6.  boolean remove(Object o);  这个方法的判断方法为(o==null ? e==null :o.equals(e))  if this collection contains one or more such elements.  Returns <tt>true</tt> if this collection contained the specified element (or equivalently, if this collection changed as a result of the call). 通过这个方法的判断方法,可以用一个空的o对象来去除,collection里面的空对象。

                                                                                             

7. boolean containsAll(Collection <?> c)  ; Returns <tt>true</tt> if this collection contains all of the elements
                                                                                                                                                 

   * in the specified collection. 判断collection 里面是否包含了c

                                                                                            

 8. boolean  removeAll(Collection<?> c); <tt>true</tt> if this collection changed as a result of the call,为啥判断为true的方法是collection改变,如果只c里面的元素没有删除干净呢。

                                                                                            

 9. boolean retainAll(Collection<?> c); Retains only the elements in this collection that are contained in the  specified collection (optional operation).<tt>true</tt> if this collection changed as a result of     the call.

                                                                                             

10. hashcode();

                                                                                             

11. boolean equals(Object c);

                                                                                             

12. clear();


13. toArray(); 


  1. import java.lang.reflect.Array;  
  2. import java.util.*;  
  3. public class Collection {  
  4.         public static void main(String args[])  
  5.         {  
  6.             ArrayList list=new ArrayList();  
  7.             list.add(1);  
  8.             list.add(2);  
  9.             list.add(3);  
  10.             //利用 toArray 把窗口转成数组  
  11.             //Integer integerArray[]=(Integer [])list.toArray();//这种写法是错误的. toArray只能转换成Object 的数组  
  12.             //Object ObjectArray[]=list.toArray();  
  13.               
  14.             /* 
  15.              * 下面说一个另一种toArray方式的用法  ,T[] toArray(T a[]) 是转换成相应类型的数组,这种个转换要有个前提,就是Arraylist中存入的数据就是这种类型的,这样才能转换成类型数组,不能再转换成其他类型 
  16.              *  
  17.              * */  
  18.             ArrayList<Integer>newlist=new ArrayList<Integer>();  
  19.             newlist.add(1);  
  20.             newlist.add(2);  
  21.             newlist.add(3);  
  22.             newlist.add(4);  
  23.             //正确写法  ,将容器里面存放的类型转成相应的数组  
  24.             Integer IntegerArray[]=newlist.toArray(new Integer[4]);    
  25.             //下面的方法是错误的  
  26.             //Long  LongArray[]=newlist.toArray(new Long[4]); //这个里面含有类型转换,故错误   
  27.               
  28.               
  29.         }  
  30. }  
  31. /* 
  32.  *                  记住一条..  toArray 只能进行数组的转换,而不能进行类型的转换 
  33.  * */                                       2011/10/25  21:19:38  

    api 文档说明 

toArray 
<T> T[] toArray(T[] a)返回包含此 collection 中所有元素的数组;返回数组的运行时类型与指定数组的运行时类型相同。如果指定的数组能容纳该 collection,则返回包含此 collection 元素的数组。否则,将根据指定数组的运行时类型和此 collection 的大小分配一个新数组。 
如果指定的数组能容纳 collection 并有剩余空间(即数组的元素比 collection 的元素多),那么会将数组中紧跟在 collection 末尾的元素设置为 null。(这对确定 collection 的长度很有用,但只有 在调用方知道此 collection 没有包含任何 null 元素时才可行。) 

如果此 collection 对其迭代器返回的元素顺序做出了某些保证,那么此方法必须以相同的顺序返回这些元素。

像 toArray 方法一样,此方法充当了基于数组的 API 与基于 collection 的 API 之间的桥梁。更进一步说,此方法允许在输出数组的运行时类型上进行精确控制,并且在某些情况下,可以用来节省分配开销。 

假定 l 是只包含字符串的一个已知 List。以下代码用来将该列表转储到一个新分配的 String 数组: 

     String[] x = (String[]) v.toArray(new String[0]); 
注意,toArray(new Object[0]) 和 toArray() 在功能上是相同的。 


参数: 
a - 存储此 collection 元素的数组(如果其足够大);否则,将为此分配一个具有相同运行时类型的新数组。 
返回: 
包含此 collection 元素的数组 
抛出: 
ArrayStoreException - 指定数组的运行时类型不是此 collection 每个元素运行时类型的超类型。 
NullPointerException - 如果指定的数组为 null。


oArray(T[] a)方法有个比较怪异的地方: 

Java代码   收藏代码
  1. List l=new ArrayList();     
  2. l.add("a");     
  3. l.add("b");     
  4. String[] strs=new String[4];//比List多2个元素     
  5. for(int i=0;i<strs.length;i++){//填充4个字符串"x”     
  6.      strs[i]="x";     
  7. }     
  8. String[] newStrs=(String[]) l.toArray(strs);  
  9. System.out.println(newStrs==strs);//为了确定是否传入的参数对象和返回的是同一个对象。  
  10. for(int i=0;i<strs.length;i++){     
  11.    System.out.println(strs[i]);     
  12. }  
返回值分别是true

             a

             b 

              null

              x

 如果目标数字的大小大于collection的大小,则会在最后后面加一个null;

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



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值