package com.wyq.StringBuffer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Vector;
public class TestList {
public static void main(String[] args) {
/**
* 类实现接口,同时这里使用到了多态
*/
//list接口new实现类ArrayList的
List arrList = new ArrayList();
//接口List接口new实现类Vector()
List vect = new Vector();
//List接口new实现类LinkedList()
List linkedList = new LinkedList();
/**
* list解口的方法
*/
arrList.add("java");
arrList.add("hello");
arrList.add("world");
arrList.add(123);
arrList.add(0, "今天的天气很好。");
System.out.println(arrList);
// System.out.println(arrList.remove(0));
// System.out.println(arrList.remove("java"));
// System.out.println(arrList.remove(1));
// System.out.println(arrList.remove(123));
/**
* 这种情况会报错:
* java.lang.IndexOutOfBoundsException: Index: 123, Size: 2
* 报错原因是这中删除方式是按照索引的值去删除的,而索引123已经超出了界限
* 下边这种就是正确的
* 或者上边的那种也是正确的
*/
System.out.println(arrList.remove(new Integer(123)));
System.out.println(arrList);
System.out.println("arrList.contains(123)——>"+arrList.contains(123));
System.out.println(arrList.get(1));
System.out.println(arrList.size());
arrList.add(0,"Pyhotn");
System.out.println("arrList.addAll(arrList)——>"+arrList.addAll(arrList));
System.out.println(arrList);
System.out.println("arrList.equals(arrList)——>"+arrList.equals(arrList));
System.out.println("arrList.indexOf(Pyhotn)——>"+arrList.indexOf("Pyhotn"));
System.out.println(arrList.lastIndexOf("Python"));
System.out.println("arrList.set(1, go语言)——>"+arrList.set(1, "go语言"));
System.out.println(arrList);
arrList.set(0, "计算机语言");
System.out.println(arrList);
Object[] arr= new Object[10];
System.out.println(arrList.toArray(arr));
for(Object o:arr){
System.out.println("集合转成的数组的遍历——>"+o);
}
System.out.println("使用迭代器循环遍历");
Iterator it = arrList.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
System.out.println("使用普通for循环");
for(int i=0;i<arrList.size();i++){
System.out.println(arrList.get(i));
}
System.out.println("使用加强for循环");
for(Object c:arrList){
System.out.println(c);
}
/**
* list中方法的总结:
* 添加:add(),addAll(),add(index,Object)
* 删除:remove(intdex),remove(Object)
* 修改:.set(index,object)
* 判断:.equals(),contain(),lastIndexof(object),isEmpty()
* 查询:get(index),indexOf(object),size()
* 转换:将集合转换城市数组toArray(数组名)
*/
/**
* Collection接口new实现类
*/
//collection接口new实现类ArrayList
Collection colA = new ArrayList();
//collection接口new实现类Vector()
Collection colV = new Vector();
//collection接口new实现类LinkedList
Collection colL = new LinkedList();
colA.add(123);
colA.add("hello");
colA.add("world");
colA.addAll(arrList);
System.out.println(colA);
System.out.println(colA.contains(123));
System.out.println(colA.containsAll(arrList));
System.out.println(colA.equals(colA));
System.out.println(colA.isEmpty());
// System.out.println(arrList.remove(123));//在list接口中就不可以使用这种方式进行删除元素
System.out.println(colA.remove(123));//在collection中就可以使用这种方式进行删除元素
System.out.println(colA.remove("123"));
System.out.println(colA.remove(new Integer(123)));
System.out.println(colA.size());
System.out.println(colA.containsAll(colA));
Object o [] = new Object[120];
System.out.println(colA.toArray(o));
for(Object ob:o){
System.out.println("集合转换成的数组——>"+o);
}
/**
* collection的方法有:
* 增加:add(object),addAll(数组)
* 删除:remove(index),remove(object)
* 判断:isEmpty(),contain(),equal()
* 查询;size()
*/
/**
* list和Collecton的区别是:
* 1、添加:list支持按照index添加add(index,object)
* 2、修改,list具有set(index,object)
* 3、删除,list是按照索引进行删除的remove(123)
* 4、判断,list中含有lastIndexOf()
* 5、转换,collection转换成的数组存储的是集合中元素的地址,而list转换成的数组存储的是集合中元素的值
*/
}
}
collection接口和list接口的区别
猜你喜欢
转载自blog.csdn.net/wyqwilliam/article/details/93246046
今日推荐
周排行