本文都以String为例
构造方法
//无泛型
LinkedList list=new LinkedList();
//和泛型配合使用
LinkedList<String> list=new LinkedList<String>();
最好和泛型配合使用,泛型的出现的意义就是为了配合集合类使用。
注意;LinkedList和<>是紧紧在一起的,一般都是一起出现的,不分开写。即LinkedList<String>
常用方法
- 增
boolean add(E e)//相当于append
add(int index,E e)//相当于insert
- 删
clear()删除所有的值,但保存节点
E remove(int index)//删除索引对应的元素
boolean remove(E e)//删除元素
- 查
E get(int index)//根据索引查元素
int indexOf(E e)//根据元素查索引
- 改
E set(int index,E e)
- 得到链表信息
int size()
boolean contain(E e)
- 复制链表
Object clone() //返回值是引用型,但是并没有说具体是什么,所以需要强转。
LinkedList<String> mylist=(LinkedList)list.clone();
必须要显示的转换成LinkedList类型的引用。