List集合的三种遍历方式,万变不离其中

package com.sxt.homework;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

public class TestList02 {
public static void main(String[] args) {

	List list=new ArrayList();//泛型和list结合使用
	list.add(new Employee1("1001", "李白", 5000.0));
	list.add(new Employee1("1002", "李贺", 4000.0));
	list.add(new Employee1("1003", "李商隐", 3000.0));
	System.out.println(list.toString());//也可以算是遍历吧,以下面三种方式为准
	
	 //1.普通for循环遍历
	for(int i=0;i lit= list.listIterator();
	while (lit.hasNext()) {
		System.out.println(lit.next());
		
	}
	System.out.println("===================");
	//4.集合类的通用遍历方式, 用迭代器迭代(其实就只有三种,这只是变化而来)
	for(ListIterator lit2=list.listIterator(); lit2.hasNext();) {
		System.out.println(lit2.next());	
	}
	

		
}

}
class Employee1{
private String id;
private String name;
private Double salary;
public Employee1() {
super();
}
public Employee1(String id, String name, Double salary) {
super();
this.id = id;
this.name = name;
this.salary = salary;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getSalary() {
return salary;
}
public void setSalary(Double salary) {
this.salary = salary;
}
@Override
public String toString() {
return “Employee1 [编号=” + id + “, 姓名=” + name + “, 工资=” + salary + “]”;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Employee1 other = (Employee1) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}

}

你可能感兴趣的:(list,Java,JavaSE,集合)