java 集合二 List
List集合
特点:有序、有下标、元素可以重复
List子接口的使用
字符
public class Demo1 {
public static void main(String[] args) {
//新建集合对象
List list = new ArrayList();
//添加元素
list.add("小米");
list.add("苹果");
list.add(0,"华为");
System.out.println("元素个数:"+list.size());
System.out.println(list.toString());
//删除元素
// list.remove("苹果");
// list.remove(0);
// System.out.println("删除之后:"+list.size());
// System.out.println(list.toString());
//遍历
//for
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
//增强for
for (Object object : list) {
System.out.println(object);
}
//迭代器
Iterator it = list.iterator();
while (it.hasNext()){
System.out.println(it.next());
}
//列表迭代器,和Iterator的区别:ListIterator可以向前或向后遍历\添加\删除\修改元素
ListIterator lit = list.listIterator();
System.out.println("------从前往后------");
while (lit.hasNext()){
System.out.println(lit.nextIndex()+":"+lit.next());
}
System.out.println("------从后往前------");
//通过上面一轮的遍历,此时"指针"已经停留在了集合的最后一位
while (lit.hasPrevious()){
System.out.println(lit.previousIndex()+":"+lit.previous());
}
//判断
System.out.println(list.contains("苹果"));
System.out.println(list.isEmpty());
//获取元素位置
System.out.println(list.indexOf("华为"));
}
}
数字
public class Demo1 {
public static void main(String[] args) {
//新建集合对象
List list = new ArrayList();
//集合不能存储基本数据类型
//所以当添加数字数据时,会自动装箱
list.add(20);
list.add(30);
list.add(40);
list.add(50);
list.add(60);
System.out.println("元素个数:"+list.size());
System.out.println(list.toString());
//删除操作(删除元素20)
//方法一
list.remove(0);
//方法二
list.remove((Object) 20);
//方法三
list.remove(new Integer(20));
System.out.println("删除之后:"+list.size());
System.out.println(list.toString());
//补充方法
//subList:返回子集合(含首不含尾)
List sl = list.subList(1, 3);
System.out.println(sl.toString());
}
}
List实现类
-
ArrayList [重点]
-
数组结构实现必须开辟连续空间,查询快、增删慢;
-
JDK1.2版本,运行效率快、线程不安全。
-
源码分析:
默认容量:DEFAULT_CAPACITY=10;
注意:没有向集合中添加任何元素时,容量为0,添加任意一个元素容量为10
(源码如下方所示) 每次扩容大小为原来的1.5倍0
存放元素的数组:elementData
实际的元素个数:size
举个例子:add()
public boolean add(E e) {
ensureCapacityInternal(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
}
private void ensureCapacityInternal(int minCapacity) {
ensureExplicitCapacity(calculateCapacity(elementData, minCapacity));
}
private static int calculateCapacity(Object[] elementData, int minCapacity) {
if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
return Math.max(DEFAULT_CAPACITY, minCapacity);
}
return minCapacity;
}
private void ensureExplicitCapacity(int minCapacity) {
modCount++;
// overflow-conscious code
if (minCapacity - elementData.length > 0)
grow(minCapacity);
}
private void grow(int minCapacity) {
// overflow-conscious code
int oldCapacity = elementData.length;
int newCapacity = oldCapacity + (oldCapacity >> 1);
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);
// minCapacity is usually close to size, so this is a win:
elementData = Arrays.copyOf(elementData, newCapacity);
}
-
-
Vector
-
数组结构实现,查询快、增删慢;
-
JDK1.0版本,运行效率慢、线程安全。
-
-
LinkedList
-
链表结构实现无需开辟连续空间,增删快、查询慢;
-
ArrayList使用
存储结构:数组 >>查找遍历速度快,增删慢
public class Demo1 {
public static void main(String[] args) {
//创建集合 size 0 容量 0
ArrayList arrayList = new ArrayList();
//添加元素
Student s1 = new Student("张三", 20);
Student s2 = new Student("李四", 20);
Student s3 = new Student("王五", 20);
arrayList.add(s1);
arrayList.add(s2);
arrayList.add(s3);
//删除元素
arrayList.remove(s1);
//遍历元素
//使用迭代器
Iterator it = arrayList.iterator();
while (it.hasNext()){
Student s = (Student) it.next();
System.out.println(s.toString());
}
//使用列表迭代器
ListIterator lit = arrayList.listIterator();
System.out.println("顺序");
while (lit.hasNext()){
Student s = (Student) lit.next();
System.out.println(s.toString());
}
System.out.println("逆序");
while (lit.hasPrevious()){
Student s = (Student) lit.previous();
System.out.println(s.toString());
}
//判断
System.out.println(arrayList.contains(s2));
System.out.println(arrayList.isEmpty());
//查找
System.out.println(arrayList.indexOf(s2));
}
}
Vector使用
Vector现在开发用得不是特别多
存储结构:数组
public class Demo1 {
public static void main(String[] args) {
//创建集合
Vector vector = new Vector();
//添加元素
vector.add("草莓");
vector.add("芒果");
vector.add("西瓜");
System.out.println("元素个数:"+ vector.size());
//删除
// vector.remove("西瓜");
// vector.remove(0);
// vector.clear();
//遍历
//使用枚举器(特有)
Enumeration en = vector.elements();
while (en.hasMoreElements()){
String o = (String) en.nextElement();
System.out.println(o);
}
//判断
vector.contains("西瓜");
vector.isEmpty();
//其它方法
vector.firstElement();
vector.lastElement();
vector.elementAt(1);
}
}
LinkedList使用
存储结构:双向链表
public class Demo1 {
public static void main(String[] args) {
//创建集合
LinkedList linkedList = new LinkedList();
//添加元素
Student s1 = new Student("张三", 20);
Student s2 = new Student("李四", 20);
Student s3 = new Student("王五", 20);
linkedList.add(s1);
linkedList.add(s2);
linkedList.add(s3);
System.out.println("元素个数:"+linkedList.size());
System.out.println(linkedList.toString());
//删除
// linkedList.remove(s1);
// linkedList.clear();
//遍历
//for遍历
for (int i = 0; i < linkedList.size(); i++) {
System.out.println(linkedList.get(i));
}
//增强for遍历
for (Object o : linkedList) {
Student s = (Student) o;
System.out.println(s.toString());
}
//迭代器遍历
Iterator it = linkedList.iterator();
while (it.hasNext()){
Student s = (Student) it.next();
System.out.println(s.toString());
}
//列表迭代器遍历
ListIterator lit = linkedList.listIterator();
while (lit.hasNext()){
Student s = (Student) lit.next();
System.out.println(s.toString());
}
//判断
System.out.println(linkedList.contains(s1));
System.out.println(linkedList.isEmpty());
//获取
System.out.println(linkedList.indexOf(s1));
}
}