1.各集合的遍历举例
1.1 Collection集合遍历
public class Phone {
String brand;
public Phone() {
}
public Phone(String brand) {
this.brand = brand;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
}
//集合转换数组后遍历
public class Dollections {
public static void main(String[] args) {
Collection list = new ArrayList();
list.add(new Phone("华为"));
list.add(new Phone("小米"));
list.add(new Phone("魅族"));
Object[] arr = list.toArray();
for (int i = 0; i < arr.length; i++) {
Phone cell =(Phone) arr[i];
System.out.println(cell.brand);
}
}
}
//迭代器遍历
public class Dollections {
public static void main(String[] args) {
Collection list = new ArrayList();
list.add(new Phone("华为"));
list.add(new Phone("小米"));
list.add(new Phone("魅族"));
Iterator iterator = list.iterator();
while (iterator.hasNext()){
Object next = iterator.next();
Phone cell1 = (Phone) next;
System.out.println(cell1.getBrand());
}
}
}
1.2 List集合遍历
public class Student {
String name;
int age;
public Student() {
}
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 Dollections {
public static void main(String[] args) {
List list1 = new ArrayList();
list1.add(new Student("张三", 20));
list1.add(new Student("李四", 22));
list1.add(new Student("王五", 21));
Iterator iterator1 = list1.iterator();
while (iterator1.hasNext()){
Object next = iterator1.next();
Student stu = (Student) next;
System.out.println(stu.getName()+" "+stu.getAge());
}
}
//size()和get()方法结合进行遍历
Iterator iterator1 = list1.iterator();
for (int i = 0; i < list1.size(); i++) {
Object o = list1.get(i);
Student student = (Student) o;
System.out.println(student.getName()+" "+student.getAge());
}
//列表迭代器进行遍历
ListIterator listIterator = list1.listIterator();
while (listIterator.hasNext()){
Object o = listIterator.next();
Student student1 = (Student) o;
System.out.println(student1.getName()+" "+student1.getAge());
}
1.3 Vector遍历
public static void main(String[] args) {
Vector students = new Vector();
students.add(new Student("张", 11));
students.add(new Student("李", 12));
students.add(new Student("王", 13));
//迭代遍历
Iterator iterator = students.iterator();
while (iterator.hasNext()){
Object o = iterator.next();
Student student = (Student) o;
System.out.println(student.getName()+" "+student.getAge());
}
//vector自带迭代器遍历
Enumeration elements = students.elements();
while (elements.hasMoreElements()){
Object o = elements.nextElement();
Student student = (Student) o;
System.out.println(student.getName()+" "+student.getAge());
}
//size()和get()方法结合进行遍历
for (int i = 0; i < students.size(); i++) {
Object o = students.get(i);
Student stu = (Student) o;
System.out.println(stu.getName()+" "+stu.getAge());
}
}
2.其他注意点
2.1 并发修改异常
原因:在用Iterator迭代器迭代时,迭代器依赖于集合,我们往集合中添加好元素后,获取迭代器,这时迭代器就已经获取到了元素的个数,此时如在遍历时给集合中添加元素,那迭代器就会发生混乱,从而报错ConcurrentModificationException
解决方法:(1)采用迭代器遍历元素,在遍历的途中我们用迭代器自带的添加或删除的方法添加或删除元素,就不会出现并发修改异常
(2)采用for循环(size()和get()方法结合进行遍历)
public static void main(String[] args) {
List list = new ArrayList();
list.add("I");
list.add("love");
list.add("China");
ListIterator listIterator = list.listIterator();
while (listIterator.hasNext()) {
Object obj = listIterator.next();
String str = (String) obj;
if (str.equals("I")) {
// list.add("really");
listIterator.add("really");//采用迭代器遍历元素,在遍历的途中我们用迭代器自带的添加或删除的方法添加或删除元素,就不会出现并发修改异常
}
}
//采用for循环
for(int i=0;i<list.size();i++){
Object o = list.get(i);
String string= (String) o;
if(string.equals("I")){
list.add("really");
}
}
}
2.2 反向遍历之前先要正向遍历(把指针移至最后)
public static void main(String[] args) {
List arrayList = new ArrayList();
arrayList.add(111);
arrayList.add(222);
arrayList.add(333);
arrayList.add(444);
ListIterator listIterator = arrayList.listIterator();
while (listIterator.hasNext()) {
Object o = listIterator.next();
System.out.println(o);
}
//反向遍历之前,先要正向遍历
ListIterator listIterator1 = arrayList.listIterator();
while (listIterator1.hasPrevious()) {
Object o = listIterator1.previous();
System.out.println(o);
}
}