您的位置:首页 > 编程语言 > Java开发

java 集合框架二(list)

2016-05-10 10:55 471 查看
/*
Collection:
|--List:集合中的元素是有序的,可重复的,有索引。
|--Set:集合中的元素是无序的,不可重复的,无索引。

List中的特有方法:
增
add(index,element);
addAll(index,Collection);
删
remove(index);
改
set(index,element);
查
get(index);
subList(start,end);
listIterator();
其他:
indexOf(obj);//通过indexOf获取对象的位置

List sub=al.subList(1,3);//获取子列表

List集合特有的迭代器ListIterator,它是Iterator的子接口。

在迭代时,不能通过集合对象的方式来操作集合中的元素。
因为会出现并发异常,即ConcurrentModificationException异常。

所以,在迭代时,只能使用迭代器的方法操作元素,可是Iterator迭代器的方法有限,
所以想要增删改查等更全面的方法,就需要使用其子接口,即ListIterator。
该接口只能通过List集合的ListIterator方法获取。
*/
import java.util.*;
class CollectionList
{
public static void main(String[] args)
{
method_1();
//System.out.println("Hello World!");
}
public static void method_1()
{
ArrayList al=new ArrayList();
al.add("hello1");
al.add("hello2");
al.add("hello3");
al.add("hello4");
sop(al);

ListIterator li=al.listIterator();
while(li.hasNext())
{
Object obj=new Object();
obj=li.next();
if(obj.equals("hello2"))
{
li.add("Hello World");
}
}
sop(al);
sop(al.get(1));

}

public static void sop(Object obj)
{
System.out.println(obj);
}
}

<img src="https://img-blog.csdn.net/20160510105412195?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java list集合