Java集合框架:list集合
List接口是Collection接口的子接口,存放的元素有序且允许重复
List集合类常用的有:ArrayList(重点) LinkedList Vector(知道即可)
list中特有的方法
描述 | |
---|---|
void add(int index,E element) | 在此集合中的指定位置插入指定的元素 |
E remove(int index) | 删除指定索引处的元素,返回被删除的元素 |
E set(int index,E element) | 修改指定索引处的元素,返回被修改的元素 |
E get(int index) |
List<String> list = new ArrayList();
list.add("aa");
list.add("bb");
list.add("cc");
list.add("dd");
System.out.println(list);
//在索引2的位置添加数据xx
list.add(2, "xx");
System.out.println(list);
//删除索引为2的位置的数据 返回被删除的数据xx
String str01 = list.remove(2);
System.out.println(list);
System.out.println(str01);
//把索引为3的位置的数据 改为ff 返回被修改的dd
String str02 = list.set(3, "ff");
System.out.println(list);
System.out.println(str02);
//获取索引0位置的数据
String str03 = list.get(0);
System.out.println(str03);
LinkedList中特有的方法
说明 | |
---|---|
public void addFirst(E e) | 在该列表开头插入指定的元素 |
public void addLast(E e) | 将指定的元素追加到此列表的末尾 |
public E getFirst() | 返回此列表中的第一个元素 |
public E getLast() | 返回此列表中的最后一个元素 |
public E removeFirst() | 从此列表中删除并返回第一个元素 |
public E removeLast() |
创建ArrayList对象
ArrayList list = new ArrayList();
创建对象后,我们可以使用使用Collection接口中的方法对ArrayList的各种操作。这些方法是定义在Collection接口中的方法,所以Collection接口中其他集合类都可以通用,包括List中的几个实现类。
Collection接口中的方法
//添加元素。
boolean add(Object element);
//移除指定元素。
boolean remove(Object element);
//获取元素个数。
int size();
package cn.xtnotes.u3;
import java.util.ArrayList;
import cn.xtnotes.pojo.Stu;
public class list_1 {
public static void main(String[] args) {
ArrayList a1=new ArrayList();
Stu stu1=new Stu("一号",15); // 使用前几节课的stu类
Stu stu2=new Stu("二号",19);
Stu stu3=new Stu("三号",16);
//在集合中添加对象
a1.add(stu1);
a1.add(stu2);
a1.add(stu3);
System.out.println(a1.size()); //输出集合中的个数
//删除数据中的元素
a1.remove(stu1);
System.out.println(a1.size());
}
}
//判断此collection中是否为空
boolean isEmpty();
//判断collection是否包含指定元素。
boolean contains(Object obj);
//判断此collection是否包含指定collection中的所有元素。
boolean contains(Collection c);
//将指定collection中的所有元素添加到此collection中
boolean addAll(Collection c);
//移除此collection中那些也包含在指定collection中的所有元素。
boolean removeAll(Collection c);
//移除collection中所有的元素。
void clear();
//仅保留此collection中那些也包含在指定collection的元素
boolean retainAll(Collectionc);
//把此collection转成数组。
Object[]toArray();
package cn.xtnotes.u3;
import java.util.ArrayList;
import cn.xtnotes.pojo.Stu;
public class list_2 {
public static void main(String[] args) {
ArrayList a1=new ArrayList();
ArrayList a2=new ArrayList();
Stu stu1=new Stu("一号",1);
Stu stu2=new Stu("二号",1);
Stu stu3=new Stu("三号",1);
Stu stu4=new Stu("四号",1);
//在集合中添加元素
a1.add(stu1);
a2.add(stu2);
a2.add(stu3);
a2.add(stu4);
System.out.println(a1.isEmpty()); //判断集合是否为空(布尔)
System.out.println(a1.contains(stu1)); //判断a1中是否包含stu1(布尔)
a1.addAll(a2); //将a2中的元素全部添加到a1中
System.out.println(a1.size());
a1.clear();
System.out.println(a1.size());
Object[] o=a2.toArray(); //将a2转换为数组
}
}
list索引
package cn.xtnotes.u3;
import java.util.ArrayList;
public class list_3 {
public static void main(String[] args) {
ArrayList a1=new ArrayList();
a1.add("s");
a1.add("a");
a1.add("f");
a1.add("b");
a1.add("d");
System.out.println(a1.get(3)); //索引从0开始,顺序由添加顺序决定
System.out.println("---------");
for(int i=0;i<a1.size();i++) {
System.out.println(a1.get(i));
}
}
}
ArrayList、LinkedList和Vector的区别
ArrayList | LinkedList | Vector |
底层封装数组实现,分配的是一块连续内存空间。 读取快,增删慢 线程不安全 初始大小是10扩充时扩充50% | 底层封装链表实现,分配的是不连续的内存空间。
增删快,读取慢
线程不安全
没有扩容的机制 使用首尾添加的操作 addFirst()和addLast() |
Vector是jdk1.0出现的,老版本java使用。底层封装数组实现。 增删查询速度都慢
线程安全 初始大小10扩充时扩充一倍 |
注:其他操作中,都是使用Collection接口中的方法