遍历
集合的遍历。其实就是依次获取集合中的每一个元素。
遍历方式一:先转成对象数组,再对对象数组进行遍历(少用)
package cn.itcast_01;
import java.util.ArrayList;
import java.util.Collection;
/*
* Object[] toArray():把集合转成数组,可以实现集合的遍历
*/
public class CollectionDemo3 {
public static void main(String[] args) {
// 创建集合对象
Collection c = new ArrayList();
// 添加元素
c.add("hello"); // Object obj = "hello"; 向上转型
c.add("world");
c.add("java");
// 遍历
// Object[] toArray():把集合转成数组,可以实现集合的遍历
Object[] objs = c.toArray();
for (int x = 0; x < objs.length; x++) {
// System.out.println(objs[x]);
// 我知道元素是字符串,我在获取到元素的的同时,还想知道元素的长度。
// System.out.println(objs[x] + "---" + objs[x].length());
// 上面的实现不了,原因是Object中没有length()方法
// 我们要想使用字符串的方法,就必须把元素还原成字符串
// 向下转型
String s = (String) objs[x];
System.out.println(s + "---" + s.length());
}
}
}
练习:使用转换成对象数组的方式实现对学生集合的遍历
package cn.itcast_02;
public class Student {
// 成员变量
private String name;
private int age;
// 构造方法
public Student() {
super();
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
// 成员方法
// getXxx()/setXxx()
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;
}
}
package cn.itcast_02;
import java.util.ArrayList;
import java.util.Collection;
/*
* 练习:用集合存储5个学生对象,并把学生对象进行遍历。
*
* 分析:
* A:创建学生类
* B:创建集合对象
* C:创建学生对象
* D:把学生添加到集合
* E:把集合转成数组
* F:遍历数组
*/
public class StudentDemo {
public static void main(String[] args) {
// 创建集合对象
Collection c = new ArrayList();
// 创建学生对象
Student s1 = new Student("林青霞", 27);
Student s2 = new Student("风清扬", 30);
Student s3 = new Student("令狐冲", 33);
Student s4 = new Student("武鑫", 25);
Student s5 = new Student("刘晓曲", 22);
// 把学生添加到集合
c.add(s1);
c.add(s2);
c.add(s3);
c.add(s4);
c.add(s5);
// 把集合转成数组
Object[] objs = c.toArray();
// 遍历数组
for (int x = 0; x < objs.length; x++) {
// System.out.println(objs[x]);
Student s = (Student) objs[x];
System.out.println(s.getName() + "---" + s.getAge());
}
}
}
遍历方式二:使用迭代器进行迭代
package cn.itcast_03;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
/*
* Iterator iterator():迭代器,集合的专用遍历方式
* Object next():获取元素,并移动到下一个位置。
* NoSuchElementException:没有这样的元素,因为你已经找到最后了。
* boolean hasNext():如果仍有元素可以迭代,则返回 true。(
*/
public class IteratorDemo {
public static void main(String[] args) {
// 创建集合对象
Collection c = new ArrayList();
// 创建并添加元素
// String s = "hello";
// c.add(s);
c.add("hello");
c.add("world");
c.add("java");
// Iterator iterator():迭代器,集合的专用遍历方式
Iterator it = c.iterator(); // 实际返回的肯定是子类对象,这里是多态
// Object obj = it.next();
// System.out.println(obj);
// System.out.println(it.next());
// System.out.println(it.next());
// System.out.println(it.next());
// System.out.println(it.next());
// 最后一个不应该写,所以,我们应该在每次获取前,如果有一个判断就好了
// 判断是否有下一个元素,有就获取,没有就不搭理它
// if (it.hasNext()) {
// System.out.println(it.next());
// }
// if (it.hasNext()) {
// System.out.println(it.next());
// }
// if (it.hasNext()) {
// System.out.println(it.next());
// }
// if (it.hasNext()) {
// System.out.println(it.next());
// }
// if (it.hasNext()) {
// System.out.println(it.next());
// }
// 最终版代码
while (it.hasNext()) {
// System.out.println(it.next());
String s = (String) it.next();
System.out.println(s);
}
}
}
练习(迭代器的方式遍历自定义学生对象)
package cn.itcast_03;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
/*
* 练习:用集合存储5个学生对象,并把学生对象进行遍历。用迭代器遍历。
*
* 注意:
* A:自己的类名不要和我们学习的要使用的API中的类名相同。
* B:复制代码的时候,很容易把那个类所在的包也导入过来,容易出现不能理解的问题。
*/
public class IteratorTest {
public static void main(String[] args) {
// 创建集合对象
Collection c = new ArrayList();
// 创建学生对象
Student s1 = new Student("林青霞", 27);
Student s2 = new Student("风清扬", 30);
Student s3 = new Student("令狐冲", 33);
Student s4 = new Student("武鑫", 25);
Student s5 = new Student("刘晓曲", 22);
// 把学生添加到集合中
c.add(s1);
c.add(s2);
c.add(s3);
c.add(s4);
c.add(s5);
// 遍历
Iterator it = c.iterator();
while (it.hasNext()) {
// System.out.println(it.next());
Student s = (Student) it.next();
System.out.println(s.getName() + "---" + s.getAge());
}
}
}
package cn.itcast_03;
public class Student {
// 成员变量
private String name;
private int age;
// 构造方法
public Student() {
super();
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
// 成员方法
// getXxx()/setXxx()
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;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
}
注意:不要多次使用it.next()方法,因为每次使用都是访问一个对象。
方式三:用JDK5的增强for循环进行遍历
package cn.itcast_01;
import java.util.ArrayList;
import java.util.List;
/*
* JDK5的新特性:自动拆装箱,泛型,增强for,静态导入,可变参数,枚举
*
* 增强for:是for循环的一种。
*
* 格式:
* for(元素数据类型 变量 : 数组或者Collection集合) {
* 使用变量即可,该变量就是元素
* }
*
* 好处:简化了数组和集合的遍历。
*
* 弊端: 增强for的目标不能为null。
* 如何解决呢?对增强for的目标先进行不为null的判断,然后在使用。
*/
public class ForDemo {
public static void main(String[] args) {
// 定义一个int数组
int[] arr = { 1, 2, 3, 4, 5 };
for (int x = 0; x < arr.length; x++) {
System.out.println(arr[x]);
}
System.out.println("---------------");
// 增强for
for (int x : arr) {
System.out.println(x);
}
System.out.println("---------------");
// 定义一个字符串数组
String[] strArray = { "林青霞", "风清扬", "东方不败", "刘意" };
// 增强for
for (String s : strArray) {
System.out.println(s);
}
System.out.println("---------------");
// 定义一个集合
ArrayList<String> array = new ArrayList<String>();
array.add("hello");
array.add("world");
array.add("java");
// 增强for
for (String s : array) {
System.out.println(s);
}
System.out.println("---------------");
List<String> list = null;
// NullPointerException
// 这个s是我们从list里面获取出来的,在获取前,它肯定还好做一个判断
// 说白了,这就是迭代器的功能
if (list != null) {
for (String s : list) {
System.out.println(s);
}
}
// 增强for其实是用来替代迭代器的
//ConcurrentModificationException
// for (String s : array) {
// if ("world".equals(s)) {
// array.add("javaee");
// }
// }
// System.out.println("array:" + array);
}
}
练习:多种循环遍历字符串集合
package cn.itcast_01;
import java.util.ArrayList;
import java.util.Iterator;
/*
* ArrayList存储字符串并遍历。要求加入泛型,并用增强for遍历。
* A:迭代器
* B:普通for
* C:增强for
*/
public class ArrayListDemo {
public static void main(String[] args) {
// 创建集合对象
ArrayList<String> array = new ArrayList<String>();
// 创建并添加元素
array.add("hello");
array.add("world");
array.add("java");
// 遍历集合
// 迭代器
Iterator<String> it = array.iterator();
while (it.hasNext()) {
String s = it.next();
System.out.println(s);
}
System.out.println("------------------");
// 普通for
for (int x = 0; x < array.size(); x++) {
String s = array.get(x);
System.out.println(s);
}
System.out.println("------------------");
// 增强for
for (String s : array) {
System.out.println(s);
}
}
}
练习:多种方式遍历自定义学生对象
package cn.itcast_01;
import java.util.ArrayList;
import java.util.Iterator;
/*
* 需求:ArrayList存储自定义对象并遍历。要求加入泛型,并用增强for遍历。
* A:迭代器
* B:普通for
* C:增强for
*
* 增强for是用来替迭代器。
*/
public class ArrayListDemo2 {
public static void main(String[] args) {
// 创建集合对象
ArrayList<Student> array = new ArrayList<Student>();
// 创建学生对象
Student s1 = new Student("林青霞", 27);
Student s2 = new Student("貂蝉", 22);
Student s3 = new Student("杨玉环", 24);
Student s4 = new Student("西施", 21);
Student s5 = new Student("王昭君", 23);
// 把学生对象添加到集合中
array.add(s1);
array.add(s2);
array.add(s3);
array.add(s4);
array.add(s5);
// 迭代器
Iterator<Student> it = array.iterator();
while (it.hasNext()) {
Student s = it.next();
System.out.println(s.getName() + "---" + s.getAge());
}
System.out.println("---------------");
// 普通for
for (int x = 0; x < array.size(); x++) {
Student s = array.get(x);
System.out.println(s.getName() + "---" + s.getAge());
}
System.out.println("---------------");
// 增强for
for (Student s : array) {
System.out.println(s.getName() + "---" + s.getAge());
}
}
}
package cn.itcast_01;
public class Student {
// 成员变量
private String name;
private int age;
// 构造方法
public Student() {
super();
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
// 成员方法
// getXxx()/setXxx()
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;
}
}