前言:Collection入门简介及基本增删改查方法。
原文出处:http://blog.csdn.net/u014158743/article/details/52628972
/*
Collection:
List:存储的对象是有序的(存储的顺序和添加的顺序是一致的),可以重复的
ArrayList:底层使用的数据结构是数组,线程不安全的,查找速度快,增删速度慢
LinkedList:底层使用的数据结构是链表,线程不安全的,s查找速度慢,增删速度快
Vector:底层使用的数据结构是数组,线程安全的,查找速度快,增删速度慢,被ArrayList替代了
Set:存储的对象是无序的,不可以重复的
List:特有方法,可以操作下标
增:
void add(int index, E element)
boolean addAll(int index, Collection<? extends E> c)
删
E remove(int index)
改
E set(int index, E element)
查
ListIterator<E> listIterator()
返回此列表元素的列表迭代器(按适当顺序)。
ListIterator<E> listIterator(int index)
List<E> subList(int fromIndex, int toIndex)
E get(int index)
*/
import java.util.*;
class Demo
{
public static void main(String[] args)
{
List col = new ArrayList();
col.add("java01");
col.add("java02");
col.add("java03");
col.add(0,"hello");//在指定的位置上添加对象
sop(col);
//col.remove(0);// 删除指定位置上的对象
//sop(col);
col.set(0,"world");//修改指定位置上的对象
sop(col);
Object obj = col.get(0);//根据下标获取对象
sop(obj);
List list= col.subList(1,3);//截取子列表
sop(list);
Iterator ite = col.iterator();
while(ite.hasNext())
{
sop(ite.next());
}
for(Iterator ites = col.iterator();ites.hasNext();)
{
sop(ites.next());
}
for(int i=0;i<col.size();i++)
{
Object objs = col.get(i);
sop(objs);
}
while(!col.isEmpty())
{
sop(col.remove(0));
}
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}