//构建类A
class A{int a=0;
String str=null;
public A(int a,String str){
this.a=a;
this.str=str;}}
public class ListTest {//初始化一个ArrayList实例,构造一个包含指定 A 的元素的列表static List<A> a=new ArrayList<A>();
public ListTest(int a1,String str){
A b=new A(a1,str);
a.add(b);//将指定的元素添加到a列表的尾部}
public staticvoidergodic(){//使用增强型for循环遍历a列表,其中a1可理解为一个临时的变量,用以存储和读取列表a从上到下中的每一个指定元素for(A a1:a){
System.out.println(a1.a+a1.str);}}
public staticvoidmain(String args[]){
ListTest b1=new ListTest(1,"这是第一个对象");
ListTest b2=new ListTest(2,"这是第二个对象");//调用遍历方法ergodic();}}