package ArrayListDemo;
import java.util.ArrayList;
import java.util.Iterator;
public class ArrayListDemo1 {
public static void main(String[] args) {
//存储字符串并遍历
//创建集合对象
ArrayList array=new ArrayList();
//创建元素对象并添加元素
array.add("hello");
array.add("world");
array.add("java");
//遍历1
Iterator it=array.iterator();
while(it.hasNext()) {
String s=(String)it.next();
System.out.println(s);
}
//遍历2
for(int x=0;x
package ArrayListDemo;
import java.util.ArrayList;
import java.util.Iterator;
public class ArrayListDemo2_student {
public static void main(String[] args) {
//创建集合对象
ArrayList array=new ArrayList();
//创建学生对象
Student s1=new Student("白敬亭",22);
Student s2=new Student("goodbai",23);
Student s3=new Student("ruilin",25);
Student s4=new Student("yundi",39);
//添加元素
array.add(s1);
array.add(s2);
array.add(s3);
array.add(s4);
//遍历
Iterator it=array.iterator();
while(it.hasNext()) {
Student s=(Student)it.next();
System.out.println(s.getName()+"---"+s.getAge());
}
//遍历2
for(int x=0;x