【我的Java笔记】增强for循环 & ArrayList集合储存自定义对象并遍历的五种方法

增强for循环

1.增强for循环的作用:

在集合中,增强for循环的作用是用来代替迭代器

2.优点:书写格式简单,代码量少

3.弊端:遍历数组或集合的时候,数组对象或者集合对象不能为null(需加上非空判断)

注:在Java中,只要是对象,那么一定要给对象做非空判断,避免出现空指针异常

4.书写格式:

for(数组或者集合中的数据类型  变量名:数组或者集合对象){

变量的输出;



例:

import java.util.ArrayList;

public class Test2 {
	public static void main(String[] args) {
		//创建ArrayList集合对象
		ArrayList al = new ArrayList();
		//给集合中添加元素
		al.add("hello");
		al.add("world");
		al.add("java");
		
		//给集合对象进行非空判断
		if(al!=null) {
			//使用增强for循环遍历集合
			for(String str:al) {
				System.out.println(str);
			}
		}
		
	}
}



【我的Java笔记】增强for循环 & ArrayList集合储存自定义对象并遍历的五种方法_第1张图片









ArrayList集合储存自定义对象并遍历的五种方法


1.使用集合遍历(toArray()方法):

import java.util.ArrayList;

//测试类
public class StudentTest {
	public static void main(String[] args) {
		//创建学生类对象
		Student s1 = new Student("国际米兰",14);
		Student s2 = new Student("罗马",56);
		Student s3 = new Student("贝尔格莱德",45);
		
		//创建ArrayList集合对象
		ArrayList al = new ArrayList();
		al.add(s1);
		al.add(s2);
		al.add(s3);
		
		//使用toArray()方法将集合转为数组
		Object [] obj = al.toArray();
		//遍历数组
		for(int i=0;i


【我的Java笔记】增强for循环 & ArrayList集合储存自定义对象并遍历的五种方法_第2张图片




2.Collection集合的迭代器进行集合遍历(Iterator iterator()方法)

import java.util.ArrayList;
import java.util.Iterator;

//测试类
public class StudentTest {
	public static void main(String[] args) {
		
		System.out.println("使用Collection集合的迭代器Iterator iterator()方法进行集合遍历的结果:");
		
		//创建学生类对象
		Student s1 = new Student("国际米兰",14);
		Student s2 = new Student("罗马",56);
		Student s3 = new Student("贝尔格莱德",45);
		
		//创建ArrayList集合对象
		ArrayList al = new ArrayList();
		al.add(s1);
		al.add(s2);
		al.add(s3);
		
		//获取迭代器对象
		Iterator it = al.iterator();
		while(it.hasNext()) {
			Student s = it.next();
			System.out.println(s);
		}
	}
}


//学生类
class Student{
	private String name;
	private int age;
	
	public Student() {
		super();
	}

	public Student(String name, int age) {
		super();
		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;
	}

	
	@Override
	public String toString() {
		return "["+name+","+age+"]";
	}
	
}

【我的Java笔记】增强for循环 & ArrayList集合储存自定义对象并遍历的五种方法_第3张图片






3.List集合的迭代器进行遍历(ListIterator listIterator()方法)

import java.util.ArrayList;
import java.util.ListIterator;

//测试类
public class StudentTest {
	public static void main(String[] args) {
		
		System.out.println("使用List集合的迭代器ListIterator listIterator()方法进行集合遍历的结果:");
		
		//创建学生类对象
		Student s1 = new Student("国际米兰",14);
		Student s2 = new Student("罗马",56);
		Student s3 = new Student("贝尔格莱德",45);
		
		//创建ArrayList集合对象
		ArrayList al = new ArrayList();
		al.add(s1);
		al.add(s2);
		al.add(s3);
		
		//获取迭代器对象
		ListIterator it = al.listIterator();
		while(it.hasNext()) {
			Student s = it.next();
			System.out.println(s);
		}
	}
}


//学生类
class Student{
	private String name;
	private int age;
	
	public Student() {
		super();
	}

	public Student(String name, int age) {
		super();
		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;
	}

	
	@Override
	public String toString() {
		return "["+name+","+age+"]";
	}
	
}


【我的Java笔记】增强for循环 & ArrayList集合储存自定义对象并遍历的五种方法_第4张图片






4.普通for循环遍历集合(size() 和 get(int index) 方法相结合)

import java.util.ArrayList;

//测试类
public class StudentTest {
	public static void main(String[] args) {

		System.out.println("使用普通for循环方法进行集合遍历的结果:");

		// 创建学生类对象
		Student s1 = new Student("国际米兰", 14);
		Student s2 = new Student("罗马", 56);
		Student s3 = new Student("贝尔格莱德", 45);

		// 创建ArrayList集合对象
		ArrayList al = new ArrayList();
		al.add(s1);
		al.add(s2);
		al.add(s3);

		for (int i = 0; i < al.size(); i++) {
			Student s = al.get(i);
			System.out.println(s);
		}
	}
}

// 学生类
class Student {
	private String name;
	private int age;

	public Student() {
		super();
	}

	public Student(String name, int age) {
		super();
		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;
	}

	@Override
	public String toString() {
		return "[" + name + "," + age + "]";
	}

}


【我的Java笔记】增强for循环 & ArrayList集合储存自定义对象并遍历的五种方法_第5张图片








5.增强for循环进行集合的遍历

import java.util.ArrayList;

//测试类
public class StudentTest {
	public static void main(String[] args) {

		System.out.println("使用增强for循环方法进行集合遍历的结果:");

		// 创建学生类对象
		Student s1 = new Student("国际米兰", 14);
		Student s2 = new Student("罗马", 56);
		Student s3 = new Student("贝尔格莱德", 45);

		// 创建ArrayList集合对象
		ArrayList al = new ArrayList();
		al.add(s1);
		al.add(s2);
		al.add(s3);
		
		//使用增强for循环行进遍历
		for (Student s:al) {
			System.out.println(s);
		}
	}
}

// 学生类
class Student {
	private String name;
	private int age;

	public Student() {
		super();
	}

	public Student(String name, int age) {
		super();
		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;
	}

	@Override
	public String toString() {
		return "[" + name + "," + age + "]";
	}

}


【我的Java笔记】增强for循环 & ArrayList集合储存自定义对象并遍历的五种方法_第6张图片





你可能感兴趣的:(JavaSE)