存储对象的容器,实现了对对象的一些操作,类似数组的功能
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class TestStudent {
public static void main(String[] args) {
// 创建集合
Collection collection = new ArrayList();
// 添加元素
collection.add("apple");
collection.add("orn");
collection.add("app");
System.out.println(collection);
// 删除元素
collection.remove("app");
System.out.println(collection);
// 清空集合
// collection.clear();
System.out.println("使用增强for循环遍历集合元素:");
// 遍历集合内容,增强型for循环
for (Object o : collection) {
System.out.println(o);
}
// 遍历集合内容,使用专属迭代器
// hasNext();有没有下一个元素
// next();获取下一个元素
// remove();删除当前元素
System.out.println("使用迭代器遍历集合元素:");
Iterator it = collection.iterator();
while (it.hasNext()){
String s = (String) it.next();
System.out.println(s);
// it.remove(); 删除元素
}
// 判断元素是否存在
System.out.println(collection.contains("apple"));
// 判断集合是否为空
System.out.println(collection.isEmpty());
}
}
--------------------------------
输出:
[apple, orn, app]
[apple, orn]
使用增强for循环遍历集合元素:
apple
orn
使用迭代器遍历集合元素:
apple
orn
true
false
学生类,Student.java:
public class Student {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
// 重写toString方法
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
操作类:
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class TestStudent {
public static void main(String[] args) {
// 创建集合对象
Collection collection = new ArrayList();
// 添加学生数据,只是添加元素的地址
Student s1 = new Student("dahe",18);
Student s2 = new Student("wangwei",20);
Student s3 = new Student("zhangsan",25);
collection.add(s1);
collection.add(s2);
collection.add(s3);
System.out.println(collection.toString());
// 删除
collection.remove(s1);
System.out.println(collection.toString());
// 清空元素,只是把元素从集合里删除,元素对象依然存在
// collection.clear();
// 遍历
for (Object o : collection) {
Student s = (Student) o;
System.out.println(s.toString());
}
// 迭代
Iterator it = collection.iterator();
while (it.hasNext()){
Student s = (Student) it.next();
System.out.println(s.toString());
}
}
}
-------------------------------------
输出:
[Student{name='dahe', age=18}, Student{name='wangwei', age=20}, Student{name='zhangsan', age=25}]
[Student{name='wangwei', age=20}, Student{name='zhangsan', age=25}]
Student{name='wangwei', age=20}
Student{name='zhangsan', age=25}
Student{name='wangwei', age=20}
Student{name='zhangsan', age=25}
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
public class TestStudent {
public static void main(String[] args) {
// 创建List集合对象
List list = new ArrayList();
// 添加元素
list.add("c++");
list.add("java");
list.add(0,"go");
System.out.println("元素个数:" + list.size());
System.out.println(list);
// 删除元素
list.remove(0);
System.out.println(list);
// 遍历
System.out.println("普通for:");
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
// 增强for
System.out.println("增强for:");
for (Object o : list) {
System.out.println(o);
}
// 迭代器
System.out.println("迭代器:");
Iterator it = list.iterator();
while (it.hasNext()){
System.out.println(it.next());
}
// 列表迭代器,ListIterator可以向前或者向后遍历,支持添加,删除,修改元素
System.out.println("列表迭代器:");
// 从前往后遍历
ListIterator lit = list.listIterator();
while (lit.hasNext()){
System.out.println(lit.nextIndex() + ":" + lit.next());
}
// 从后往前遍历
while (lit.hasPrevious()){
System.out.println(lit.previousIndex() + ":" + lit.previous());
}
// 判断
System.out.println(list.contains("java"));
System.out.println(list.isEmpty());
// 获取元素位置
System.out.println(list.indexOf("java"));
}
}
---------------------------------------------
输出:
元素个数:3
[go, c++, java]
[c++, java]
普通for:
c++
java
增强for:
c++
java
迭代器:
c++
java
列表迭代器:
0:c++
1:java
1:java
0:c++
true
false
1
import java.util.ArrayList;
import java.util.List;
public class TestStudent {
public static void main(String[] args) {
// 创建List集合对象
List list = new ArrayList();
// 添加元素
list.add(10);
list.add(20);
list.add(0,30);
list.add(521);
list.add(1314);
System.out.println(list);
// 返回子集合
System.out.println(list.subList(1,3));
}
}
--------------------------------------
输出:
[30, 10, 20, 521, 1314]
[10, 20]
基本同上的类方法
import java.util.ArrayList;
public class TestStudent {
public static void main(String[] args) {
// 创建集合
ArrayList arrayList = new ArrayList<>();
// 添加元素
Student s1 = new Student("唐山",12);
Student s2 = new Student("烧烤",18);
arrayList.add(s1);
arrayList.add(s2);
System.out.println(arrayList.toString());
// 删除元素
arrayList.remove(s1);
System.out.println(arrayList.toString());
}
}
-------------------------------------------
输出:
[Student{name='唐山', age=12}, Student{name='烧烤', age=18}]
[Student{name='烧烤', age=18}]
private static final int DEFAULT_CAPACITY = 10;
transient Object[] elementData; // non-private to simplify nested class access
private int size;
第一块:集合为空的时候初始化
private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
-->
public ArrayList() {
this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}
// 集合中没有添加元素的时候,容量为0
第二块:添加元素
public boolean add(E e) {
modCount++;
add(e, elementData, size);
return true;
}
-->
private void add(E e, Object[] elementData, int s) {
// 如果空间不够了,那么调用grow()进行扩容
if (s == elementData.length)
elementData = grow();
// 这里是添加元素的主要逻辑
elementData[s] = e;
size = s + 1;
}
-->
private Object[] grow() {
return grow(size + 1); // 继续跳转
}
-->
// 扩容原来的1.5倍
private Object[] grow(int minCapacity) {
int oldCapacity = elementData.length;
if (oldCapacity > 0 || elementData != DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
int newCapacity = ArraysSupport.newLength(oldCapacity,
minCapacity - oldCapacity, /* minimum growth */
oldCapacity >> 1 /* preferred growth */);
return elementData = Arrays.copyOf(elementData, newCapacity);
} else {
return elementData = new Object[Math.max(DEFAULT_CAPACITY, minCapacity)];
}
}