有序(存入和取出的顺序一致);元素有索引(角标);元素可以重复。
List arrayList = new ArrayList();
//添加元素
arrayList.add("abc1");
arrayList.add("abc2");
arrayList.add("abc3");
arrayList.add("abc4");
System.out.println(arrayList);// [abc1, abc2, abc3, abc4]
//插入元素
arrayList.add(1,"abc5");
System.out.println(arrayList);// [abc1, abc5, abc2, abc3, abc4]
//删除元素
System.out.println(arrayList.remove(1));//abc5
System.out.println(arrayList);// [abc1, abc2, abc3, abc4]
//修改元素
System.out.println(arrayList.set(1,"abc9"));// abc2
System.out.println(arrayList);// [abc1, abc9, abc3, abc4]
//获取子列表
System.out.println(arrayList.subList(1,3));// [abc9, abc3]
//获取元素
System.out.println(arrayList.get(3));// abc4
System.out.println(arrayList.get(4));// IndexOutOfBoundsException
下述代码中,集合和迭代器同时在对元素进行修改,会产生异常。也就是说,在迭代器过程中,不要使用集合操作元素,容易出现异常。
List l = new ArrayList();
l.add("abc1");
l.add("abc2");
l.add("abc3");
l.add("abc4");
Iterator i = l.iterator();
while(i.hasNext()) {
Object obj = i.next();
if(obj.equals("abc3")) {
l.add("abc9");//ConcurrentModificationException
}else{
System.out.println(obj);
}
}
可以使用Iterator的子接口ListIterator来完成在迭代中对元素进行操作。
List l = new ArrayList();
l.add("abc1");
l.add("abc2");
l.add("abc3");
l.add("abc4");
ListIterator i = l.listIterator();
while(i.hasNext()) {
Object obj = i.next();
if(obj.equals("abc3")) {
i.set("abc9");
}else{
System.out.println(obj);
}
}
System.out.println(l);// [abc1, abc2, abc9, abc4]
boolean hasPrevious():逆向遍历列表
boolean hasNext():正向遍历列表
previous():返回列表中的前一个元素
next():返回列表中的下一个元素
Vector:内部是数组数据结构,是同步的。增删,查询都很慢。
ArrayList:内部是数组数据结构,是不同步的。替代Vector。查询的速度快。
LinkedList:内部是链表数据结构,是不同步的。增删元素的速度很快。
Vector v = new Vector();
v.addElement("abc1");
v.addElement("abc2");
v.addElement("abc3");
v.addElement("abc4");
for(Enumeration enumeration = v.elements();enumeration.hasMoreElements(); ) {
System.out.println(enumeration.nextElement());
}
ps: Enumeration和Iterator的功能是重复的,都是遍历。
import java.util.LinkedList;
class Queue{
//用LinkedList做一个队列
private LinkedList linkedList;
Queue() {
linkedList = new LinkedList();
}
public void myAdd(Object obj) {
linkedList.addFirst(obj);
}
public Object myGet() {
return linkedList.removeFirst();
}
public boolean isNull() {
return linkedList.isEmpty();
}
}
public class LinkedListDemo {
public static void main(String[] args) {
Queue queue = new Queue();
queue.myAdd("abc1");
queue.myAdd("abc2");
queue.myAdd("abc3");
while(!queue.isNull()) {
System.out.println(queue.myGet());
}
}
}
import java.util.ArrayList;
import java.util.Iterator;
class Student {
private String name;
private int age;
Student() {
}
Student(String name,int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
public class ArrayListDemo {
public static void main(String[] args) {
ArrayList arrayList = new ArrayList();
arrayList.add(new Student("abc1",21));
arrayList.add(new Student("abc2",22));
arrayList.add(new Student("abc3",23));
arrayList.add(new Student("abc4",24));
for(Iterator iterator = arrayList.iterator();iterator.hasNext();) {
Student s = (Student)iterator.next();
System.out.println(s.getName()+"—————"+s.getAge());
}
}
}
首先,下面这段代码就是用迭代器遍历输出对象的name和age。
import java.util.ArrayList;
import java.util.Iterator;
class Student{
private String name;
private int age;
Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
public class Demo {
public static void main(String[] args) {
ArrayList a = new ArrayList();
a.add(new Student("zxc1",21));
a.add(new Student("zxc2",22));
a.add(new Student("zxc2",22));
a.add(new Student("zxc3",23));
a.add(new Student("zxc3",23));
Iterator iterator = a.iterator();
while(iterator.hasNext()) {
Student s = (Student)iterator.next();
System.out.println(s.getName()+"——"+s.getAge());
}
}
}
输出发现有两个对象输出的值是一样的,所以通过增加功能删除元素值相同的对象。
解决方法:在Student类中重写equals方法,在Demo类中定义一个新的getSingle方法。
import java.util.ArrayList;
import java.util.Iterator;
class Student{
private String name;
private int age;
Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public boolean equals(Object obj) {
Student s = (Student)obj;
return s.age==this.age &&
this.name==s.name;
}
}
public class Demo {
public static void main(String[] args) {
ArrayList a = new ArrayList();
a.add(new Student("zxc1",21));
a.add(new Student("zxc2",22));
a.add(new Student("zxc2",22));
a.add(new Student("zxc3",23));
a.add(new Student("zxc3",23));
a = getSingle(a);
Iterator iterator = a.iterator();
while(iterator.hasNext()) {
Student s = (Student)iterator.next();
System.out.println(s.getName()+"——"+s.getAge());
}
}
public static ArrayList getSingle(ArrayList a) {
ArrayList temp = new ArrayList();
Iterator iterator = a.iterator();
while (iterator.hasNext()) {
Object obj = iterator.next();
if (!temp.contains(obj)) {
temp.add(obj);
}
}
return temp;
}
}
重写equals方法是因为getSingle方法中的contains是根据equals来判断的,不改写之前,equals判断的是地址值,即使对象name和age相同的情况下地址值都是不一样的,因为它们都是new出来的对象,所以我们在Student类中将equals方法改写,改成只要name和age值相同就返回true。
代码示例
import java.util.ArrayList;
import java.util.Iterator;
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;
}
}
public class Demo {
public static void main(String[] args) {
ArrayList<Student> a = new ArrayList<Student>();
a.add(new Student("abc1",21));
a.add(new Student("abc2",23));
a.add(new Student("abc8",27));
a.add(new Student("abc5",26));
// 1.用for循环
for (int i = 0; i < a.size(); i++) {
Student s = a.get(i);
System.out.println(s.getName()+":"+s.getAge());
}
System.out.println("————————————————————————");
// 2.用迭代器
for(Iterator<Student> iterator = a.iterator();iterator.hasNext();) {
Student s = iterator.next();
System.out.println(s.getName()+":"+s.getAge());
}
System.out.println("————————————————————————");
// 3.用for each(增强for)循环
for (Student s : a) {
System.out.println(s.getName()+":"+s.getAge());
}
System.out.println("————————————————————————");
// 4.Lambda表达式
a.forEach(s->{
System.out.println(s.getName()+":"+s.getAge());
});
}
}