集合转数组
把集合转成数组,可以实现集合的遍历
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[] objects = c.toArray();
for (int i = 0; i < objects.length; i++){
//Object没有length方法,要想使用字符串的方法,就必须把元素还原成字符串
//向下转型
String s = (String) objects[i];
System.out.println(s + "---" + s.length());
}
}
}
Iterator iterator() 迭代器
集合专用的遍历方式
public class IteratorDemo {
public static void main(String[] args) {
Collection c = new ArrayList();
c.add("hello");
c.add("world");
c.add("java");
//Iterator是接口,返回的是子类对象,多态
//Iterator iterator() 迭代器,集合专用的遍历方式
Iterator it = c.iterator();
//Object next()获取元素,并移动到下一个位置
System.out.println(it.next());
System.out.println(it.next());
System.out.println(it.next());
}
}
如果多一个next,会报错,NoSuchElementException没有这样的元素,因为已经到最后了
public class IteratorDemo {
public static void main(String[] args) {
Collection c = new ArrayList();
c.add("hello");
c.add("world");
c.add("java");
//Iterator是接口,返回的是子类对象,多态
//Iterator iterator() 迭代器,集合专用的遍历方式
Iterator it = c.iterator();
//Object next()获取元素,并移动到下一个位置
System.out.println(it.next());
System.out.println(it.next());
System.out.println(it.next());
System.out.println(it.next());
}
}
因为需要加一个判断,在每次获取之前,如果有一个判断就好了
判断是否有下一个元素,有就获取,没有就不搭理
while循环
public class IteratorDemo {
public static void main(String[] args) {
Collection c = new ArrayList();
c.add("hello");
c.add("world");
c.add("java");
Iterator it = c.iterator();
while (it.hasNext()){
System.out.println(it.next());
}
}
}
注意it.next返回的是对象,所以进行一下向下转型才行,转换成string
public class IteratorDemo {
public static void main(String[] args) {
Collection c = new ArrayList();
c.add("hello");
c.add("world");
c.add("java");
Iterator it = c.iterator();
while (it.hasNext()){
String s = (String) it.next();
System.out.println(s);
}
}
}
for循环
注意for循环效率高,但是看起来不得劲,还是while比较好
不要多次使用it.next(),因为每次使用都是访问一个对象
public class IteratorDemo {
public static void main(String[] args) {
Collection c = new ArrayList();
c.add("hello");
c.add("world");
c.add("java");
//for循环改写
for (Iterator it = c.iterator(); it.hasNext();){
String s = (String) it.next();
System.out.println(s);
}
}
}
总结:
迭代器为什么不定义一个类,而是一个接口?
迭代器原理与源码
编译看左边,运行看右边
练习题:
需求:储存自定义对象并遍历student(name,age)
学生类
public class Student {
private String name;
private 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;
}
}
/**
* 需求:储存自定义对象并遍历student(name,age)
*
* 分析:
* A:创建学生类
* B:创建集合对象
* C:创建学生对象
* D:把学生对象添加到集合对象中
* E:遍历集合
*
* @author yhs
* @date 2020/7/23 - 14:40
*/
public class CollectionTest2 {
public static void main(String[] args) {
//创建集合对象
Collection c = new ArrayList();
//创建学生对象
Student s1 = new Student("貂蝉",25);
Student s2 = new Student("小乔",16);
Student s3 = new Student("鸡蛋糕",26);
Student s4 = new Student();
s4.setName("大乔");
s4.setAge(22);
//把学生对象添加到集合对象中
c.add(s1);
c.add(s2);
c.add(s3);
c.add(s4);
//遍历集合
Iterator it = c.iterator();
while (it.hasNext()){
Student s = (Student)it.next();
System.out.println(s.getName() + "---" + s.getAge());
}
}
}