1. 程式人生 > >【我的Java筆記】增強for迴圈 & ArrayList集合儲存自定義物件並遍歷的五種方法

【我的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<String> al = new ArrayList<String>();
		//給集合中新增元素
		al.add("hello");
		al.add("world");
		al.add("java");
		
		//給集合物件進行非空判斷
		if(al!=null) {
			//使用增強for迴圈遍歷集合
			for(String str:al) {
				System.out.println(str);
			}
		}
		
	}
}


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<Student> al = new ArrayList<Student>();
		al.add(s1);
		al.add(s2);
		al.add(s3);
		
		//使用toArray()方法將集合轉為陣列
		Object [] obj = al.toArray();
		//遍歷陣列
		for(int i=0;i<obj.length;i++) {
			//將物件obj[i]向下轉型
			Student s = (Student)obj[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+"]";
	}
	
}


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<Student> al = new ArrayList<Student>();
		al.add(s1);
		al.add(s2);
		al.add(s3);
		
		//獲取迭代器物件
		Iterator<Student> 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+"]";
	}
	
}


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<Student> al = new ArrayList<Student>();
		al.add(s1);
		al.add(s2);
		al.add(s3);
		
		//獲取迭代器物件
		ListIterator<Student> 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+"]";
	}
	
}


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<Student> al = new ArrayList<Student>();
		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 + "]";
	}

}

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<Student> al = new ArrayList<Student>();
		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 + "]";
	}

}