有什么问题
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
//1.自定义Student类,创建5个Student对象,将其存储到集合中,进行遍历(所有方式)。
class Student{
String name;
int age;
int sid;
String xb;
String aiHao;
public Student(String name, int age, int sid, String xb, String aiHao) {
this.name = name;
this.age = age;
this.sid = sid;
this.xb = xb;
this.aiHao = aiHao;
}
}
public class xsj2 {
public static void main(String[] args) {
List list = new ArrayList();
list.add(new Student("小明",18,10001,"男","打游戏"));
list.add(new Student("小强",19,10002,"男","打篮球"));
list.add(new Student("小丽",17,10003,"女","唱歌"));
list.add(new Student("张三",20,10004,"男","踢足球"));
list.add(new Student("小娟",19,10005,"女","弹钢琴"));
//集合转数组
Object[] objects = list.toArray();
for (int i = 0; i < objects.length; i++) {
System.out.println(objects[i]);
}
//集合特有的方法遍历集合元素
//通过iterator,返回的一个迭代器,迭代器可以迭代取出Collection中的元素
Iterator it = list.iterator();//多态 接口类型的引用指向了自己的实现类对象
while (it.hasNext()){
System.out.println(it.next());
}
//for-each
for (Object o : list) {
System.out.println(o);
}
//普通for语句
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
//列表迭代器 注意:仅限于List使用
ListIterator lisyIt = list.listIterator();
while (lisyIt.hasNext()) {
Object obj = lisyIt.next();
if (obj.equals("java02")) {
/* lisyIt.add("999");//添加
lisyIt.remove();//移除*/
//lisyIt.set("999");//修改
}
}
System.out.println(list);
}
private static class StudentList {
}
}