该接口下面的 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();
- import java.lang.reflect.Array;
- import java.util.*;
- public class Collection {
- public static void main(String args[])
- {
- ArrayList list=new ArrayList();
- list.add(1);
- list.add(2);
- list.add(3);
- //利用 toArray 把窗口转成数组
- //Integer integerArray[]=(Integer [])list.toArray();//这种写法是错误的. toArray只能转换成Object 的数组
- //Object ObjectArray[]=list.toArray();
- /*
- * 下面说一个另一种toArray方式的用法 ,T[] toArray(T a[]) 是转换成相应类型的数组,这种个转换要有个前提,就是Arraylist中存入的数据就是这种类型的,这样才能转换成类型数组,不能再转换成其他类型
- *
- * */
- ArrayList<Integer>newlist=new ArrayList<Integer>();
- newlist.add(1);
- newlist.add(2);
- newlist.add(3);
- newlist.add(4);
- //正确写法 ,将容器里面存放的类型转成相应的数组
- Integer IntegerArray[]=newlist.toArray(new Integer[4]);
- //下面的方法是错误的
- //Long LongArray[]=newlist.toArray(new Long[4]); //这个里面含有类型转换,故错误
- }
- }
- /*
- * 记住一条.. toArray 只能进行数组的转换,而不能进行类型的转换
- * */ 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)方法有个比较怪异的地方:
- List l=new ArrayList();
- l.add("a");
- l.add("b");
- String[] strs=new String[4];//比List多2个元素
- for(int i=0;i<strs.length;i++){//填充4个字符串"x”
- strs[i]="x";
- }
- String[] newStrs=(String[]) l.toArray(strs);
- System.out.println(newStrs==strs);//为了确定是否传入的参数对象和返回的是同一个对象。
- for(int i=0;i<strs.length;i++){
- System.out.println(strs[i]);
- }
a
b
null
x
如果目标数字的大小大于collection的大小,则会在最后后面加一个null;
-------------------------------------------------------------------------------