- 特点:有序,有下标,元素可以重复。
- 方法:
- void add(int index,Object o) //在index位置插入对象o。
- boolean addAll(int index,Collection c) //将一个集合中的元素添加到集合中的index位置。
- Object get(int index) //返回集合中指定位置的元素。
- List subList(int fromIndex,int toIndex) //返回fromIndex和Index之间的集合元素
public static void main(String args){
//先创建集合对象
List list=new ArrayList<>();
//1.添加元素
list.add("苹果");
list.add("小米");
list.add("华为");
System.out.println("元素个数:"+list.size());
System.out.println(list.toString());
//2删除元素
//list.remove("苹果");
list.remove(0);
System.out.println("删除之后:"+list.size());
System.out.println(list.toString());
//3遍历
//3.1使用for遍历
for(int i=0; i<list.size();i++){
System.out.println(list.get(i));
}
//3.2使用增强for
for(Object object :list){
System.out.println(object);
}
//3.3使用迭代器
Iterator it=list.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
//3.4使用列表迭代器和Iterator的区别,ListIterator可以向前或向后遍历,添加,删除,修改元素。
ListIterator lit=list.listIterator();
Systemo.out.println("使用列表迭代器从前往后");
while(lit.hasNext()){
System.out.println(lit.nextIndex()+":"+lit.next());
}
Systemo.out.println("使用列表迭代器从后往前");
while(lit.hasPrevious()){
System.out.println(lit.previousIndex()+":"+lit.previous());
}
//4判断
System.out.println(list.contains("苹果"));
System.out.println(list.isEmpty());
//5.获取位置
System.out.println(list.indexOf("华为"));
}
public static void main(String[] args){
//创建集合
List list=new ArrayList();
//1添加数字数据(自动装箱)
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());
//2.删除操作
//list.remove(0);
list.remove(new Integer(20));
System.out.println("删除元素:"+list.size())
System.out.println(list.toString());
//3补充方法subList:返回子集合,含头不含尾
List subList=list.subList(1,3);
System.out.println(subList.toString());
}
List实现类
- ArrayList【重点】:
- 数组结构实现,查询快,增删慢;
- JDK1.2版本,运行效率快,线程不安全。
- Vector:
- 数组结构实现,查询快,增删慢;
- JDK1.0版本,运行效率慢,线程安全。
- LinkedList:
- 链表结构实现,增删快,查询慢。
ArrayList的使用
public static void main(String[] args){
//创建集合
ArrayList arrayList=new ArrayList<>();
//1.添加元素
Student s1=new Student("刘德华",20);
Student s2=new Student("郭富城",22);
arrayList.add(s1);
arrayList.add(s2);
System.out.println("元素个数:"+arrayList.size());
System.out.println(arrayList.toString());
//2.删除元素
arrayList.remove(new Student("刘德华",20))
System.out.println("删除之后:"+arrayList.size());
//3.遍历元素【重点】
//3.1使用迭代器
Iterator it=arrayList.iterator();
while(it.hasNext()){
Student s=(Stuent)it.next();
System.out.println(s.toString());
}
//3.2使用列表迭代器逆序
while(it.hasPrevious()){
Student s=(Student)it.previous();
System.out.println(s.toString());
}
//4.判断
System.out.println(arrayList.contains(new Studnet("梁朝伟",18)));
System.out.println(arrayList.isEmpty());
//5.查找
System.out.pritln(arrayList.indexOf(new Student("梁朝伟",18)));
}
重写equals
@Override
public boolean equals(Object obj){
//1判断是不是同一个对象
if(this==obj){
return true;
}
//2.判断是否为空
if(obj==null){
return false;
}
//3判断是否是Student类型
if(obj instanceof Student){
Student s=(Student)obj;
//4.比较属性
if(this.name.equals(s.getName())&&this.age==s.getAge()){
return true;
}
}
//5不满足条件返回false
return false;
}
ArrayList
源码分析: 默认容量 DEFAULT_CAPACITY=10;
注意:如果没有向集合中添加任何元素时,容量为0,添加一个元素之后 容量10 每次扩容原来的1.5倍
存放元素的数组 elementData
size 实际的元素个数
add() 添加元素
public boolean add(E e){
ensureCapacityInternal(size+1); //Increments modCount!!
elementData[size++]=e;
return true;
}
private void ensureCapacityInternal(int minCapacity){
if(elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA){
minCapacity = Math.max(DEFAULT_CAPACITY,minCapacity);
}
ensureExplicitCapacity(minCapacity);
}
private void ensureExplicitCapacity(int minCapacity){
modCount++;
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的使用
public static void main(String[] args){
//创建集合
Vector vector=new Vector();
//1.添加元素
vector.add("草莓");
vector.add("芒果");
vector.add("西瓜");
System.out.println("元素个数:"+vector.size());
//2.删除
vector.remove(0);
vector.remove("西瓜");
vector.clear();
//3.遍历
//使用枚举器
Enumeration en=vector.elements();
while(en.hasMoreElements()){
String o=(String)en.nextElement();
System.out.println(o);
}
//4.判断
System.out.println(vector.contatis("西瓜"));
System.out.perinln(vector.isEmpty());
//5.vector其他方法
//firsetElement,lastElement,elementAt();
}
LinkedList的使用
- 链表结构实现,增删快,查询慢。
public static void main(String[] args){
//创建集合
LinkedList linkedList=new LinkedList<>();
//1.添加元素
Student s1=new Student("刘德华",20);
Student s2=new Student("郭富城",22);
Student s3=new Student("梁朝伟",18);
linkedList.add(s1);
linkedList.add(s2);
linkedList.add(s3);
System.out.println("元素个数:"+linkedList.size());
System.out.priintln(linkedList.toString());
//2.删除
linkedList.remove(new Student("刘德华",20));
System.out.println("删除之后:"+linkedList.size());
linkedList.clear();
//3.遍历
//3.1for遍历
for(int i;i<linkedList.size();i++){
System.out.println(linkedList.get(i));
}
//3.2增强for
for(Object object :linkedList){
Student s=(Student)object;
System.out.println(s.toString());
}
//3.3使用迭代器
Iterator it=linkedList.iterator();
while(it.hasNext()){
Student s=(Student)it.next();
System.out.println(s.toString();
}
ListItertor it = linkedList.listItertor();
while(it.hasNext()){
Student s=(Student)it.next();
System.out.println(s.toString());
}
//4.判断
System.out.prinln(linkedList.contains(s1);
System.out.println(linkedList.isEmpty());
//5.获取
System.out.println(linkedList.indexOf(s2));
LinkedList源码分析:
int size:集合的大小
Node first;链表的头节点
Node last;链表的尾节点
void linkLast(E e) {
final Node<E> l = last;
final Node<E> newNode = new Node<>(l, e, null);
last = newNode;
if (l == null)
first = newNode;
else
l.next = newNode;
size++;
modCount++;
}
private static class Node<E> {
E item;
Node<E> next;
Node<E> prev;
Node(Node<E> prev, E element, Node<E> next) {
this.item = element;
this.next = next;
this.prev = prev;
}
}