/*
* collection:
* List:存储的对象是有序的(集合中的对象的顺序和添加对象的顺序是一致的),可以重复的
* ArrayList:底层使用的数据结构是数组,线程不安全的,查找速度快,增删速度慢
* LinkedList:底层使用的数据结果是链表,线程不安全的,查找速度慢,增删速度块
* 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)
* */
package com.jh;
import java.util.*;
public class TestList {
public static void main(String[] args) {
List col=new ArrayList();
col.add("java01");
col.add("java02");
col.add("java03");
//下标是从0开始的
col.add(1,"hello");//在下标1的位置添加对象 输出结果[java01, hello, java02, java03]
col.remove(2);//删除下标2,就是删除java02对象
//sop(col.get(0));//获取下标为0的对象
col.set(1, "world");//把下标0的hello对象修改为world
Iterator ite=col.iterator();
while(ite.hasNext())//当返回TRUE
{
sop(ite.next()) ;
}
for(Iterator ites=col.iterator();ites.hasNext();)//FOR循环
{
sop(ites.next());
}
for(int i=0;i<col.size();i++)
{
Object obj=col.get(i);
sop(obj);
}
//如下虽然显示出来了。但是会被删除掉了
while(!col.isEmpty())
{
sop(col.remove(0));//返回被删除的对象
}
/*提示:输出的效果是一样的
* //,for循环的好处
*/// sop(col);
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}
* collection:
* List:存储的对象是有序的(集合中的对象的顺序和添加对象的顺序是一致的),可以重复的
* ArrayList:底层使用的数据结构是数组,线程不安全的,查找速度快,增删速度慢
* LinkedList:底层使用的数据结果是链表,线程不安全的,查找速度慢,增删速度块
* 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)
* */
package com.jh;
import java.util.*;
public class TestList {
public static void main(String[] args) {
List col=new ArrayList();
col.add("java01");
col.add("java02");
col.add("java03");
//下标是从0开始的
col.add(1,"hello");//在下标1的位置添加对象 输出结果[java01, hello, java02, java03]
col.remove(2);//删除下标2,就是删除java02对象
//sop(col.get(0));//获取下标为0的对象
col.set(1, "world");//把下标0的hello对象修改为world
Iterator ite=col.iterator();
while(ite.hasNext())//当返回TRUE
{
sop(ite.next()) ;
}
for(Iterator ites=col.iterator();ites.hasNext();)//FOR循环
{
sop(ites.next());
}
for(int i=0;i<col.size();i++)
{
Object obj=col.get(i);
sop(obj);
}
//如下虽然显示出来了。但是会被删除掉了
while(!col.isEmpty())
{
sop(col.remove(0));//返回被删除的对象
}
/*提示:输出的效果是一样的
* //,for循环的好处
*/// sop(col);
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}