黑马程序员技术交流社区
标题:
关于集合遍历的问题
[打印本页]
作者:
大大阳
时间:
2016-3-13 18:29
标题:
关于集合遍历的问题
class Student{
String name;
public Student(String name){
this.name = name;
}
@Override
public String toString() {
return "Student [name = " + this.name + "]";
}
}
public class Demo{
public static void main(String[] args) {
List list = new ArrayList();
list.add(new Student("赵四"));
list.add(new Student("郭德纲"));
list.add(new Student("小沈阳"));
Iterator it = list.iterator();
while (it.hasNext()) {
it.next();
Student str = list.get((String) it.next());
System.out.println(it.next());
}
}
}
出问题了 求大神指点
作者:
xgm
时间:
2016-3-13 18:59
我给你讲一下吧,
class Student{
String name;
public Student(String name){
this.name = name;
}
@Override
public String toString() {
return "Student [name = " + this.name + "]";
}
}
public class Test3{
public static void main(String[] args) {
List list = new ArrayList();
list.add(new Student("赵四"));
list.add(new Student("郭德纲"));
list.add(new Student("小沈阳"));
Iterator it = list.iterator();
/**
* Iterator对象的使用,hasNext()方法和next方法的使用
* 我画个图吧,
*
*/
while (it.hasNext()) {
/**
* 多次next指针一直在右移,当然有问题了。
* 定义一个Student引用接收值吧
*/
//it.next();
Student s = (Student) it.next();
/**
* list的get方法只有一个,是带有int参数的
*/
//Student str = list.get((String)it.next());
System.out.println(s);
}
}
}
复制代码
QQ图片20160313185651.jpg
(26.41 KB, 下载次数: 7)
下载附件
2016-3-13 18:59 上传
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2