关于Collection转型后 取值

来源:百度知道 编辑:UC知道 时间:2024/06/17 13:35:45
Collection<Integer> c = new ArrayList<Integer>();
int [] ar ={11,12,31,41,51};
for(int i :ar){
c.add(i);
}
Iterator<Integer> it = c.iterator();
while(it.hasNext()){
int j = (int) it.next();
//System.out.println(j);

}
如上 我已经把数组数据写入C了
可是我想取出C中的一个数据 比如 31
该怎么做?。。
Arraylist有get方法
可是Collection没有。。

最好这么写List<Integer> list = new ArrayList<Integer>();

collection有点太泛了,毕竟set、queue也是collection,他们是和list不同的数据结构。
arraylist就好像是list的顺序表实现方式,linkedlist是链表实现方式,但二者的操作都是一样的,只是效率不同。

- -! 谁教你这用的

一般都是这么定义的
List<Integer> list = new ArrayList<Integer>();

你吃多了撑着了,Collection是个接口,ArrayList是他的实现,所有方法都是ArrayList的,借口的方法也是ArrayList的,ArrayList的方法都有你不用,跑去用接口的方法,借口只定义基本的结构,你直接用ArrayList的方法不就好了。
ArrayList<Integer> c = new ArrayList<Integer>();
直接这么写就好饿。