A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 终结者 中级黑马   /  2013-11-19 16:43  /  1124 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

import java.util.*;
public class ListTest {
        public static void main(String[] args) {
                List list=new ArrayList();
                list.add(new Person("张三",23));
                list.add(new Person("李四",35));
                list.add(new Person("王五",45));
                list.add(new Person("张三",23));
                list.remove(3);
                for(Object obj:list){
                        Person p=(Person)list;
                        System.out.println(p);
                }
        }
}
class  Person{
        private String name;
        private int age;
        public Person(String name,int age){
                this.name=name;
                this.age=age;
        }
        public String toString(){
                return this.name+"------"+this.age;
        }
}
为什么会报错啊,求理解

评分

参与人数 1技术分 +1 黑马币 +3 收起 理由
FFF + 1 + 3 同学,细心,耐心。在代码中,是不可少的.

查看全部评分

5 个回复

倒序浏览
public class ThreadTest5 {
        public static void main(String[] args) {
                List list=new ArrayList();
                list.add(new Person("张三",23));
                list.add(new Person("李四",35));
                list.add(new Person("王五",45));
                list.add(new Person("张三",23));
                list.remove(3);
                for(Object obj:list){
                        Person p=(Person)obj;//你这里应该用obj,是你的高级for循环用错对象了
                        System.out.println(p);
                }
        }
}
class  Person{
        private String name;
        private int age;
        public Person(String name,int age){
                this.name=name;
                this.age=age;
        }
        public String toString(){
                return this.name+"------"+this.age;
        }
}

评分

参与人数 1技术分 +1 黑马币 +3 收起 理由
狼王 + 1 + 3 谢谢你的回复,论坛需要你

查看全部评分

回复 使用道具 举报
亲,你写的代码咋那么乱呢?[code]package cn.itcase;


package cn.itcase;

import java.util.ArrayList;
import java.util.List;
public class Demo6 {
    public static void main(String[] args) {
            List<Person> list=new ArrayList<Person>();//
            list.add(new Person("张三",23));
            list.add(new Person("李四",35));
            list.add(new Person("王五",45));
            list.add(new Person("张三",23));
            list.remove(3);
         
            for(Person person:list){
               
                    System.out.println(person.getName()+"\t"+person.getAge());
            }
    }
}
class  Person{
    private String name;
    private int age;
    public Person(String name,int age){
            this.name=name;
            this.age=age;
    }
    public String toString(){
            return this.name+"------"+this.age;
    }
public String getName() {
  return name;
}
public void setName(String name) {
  this.name = name;
}
public int getAge() {
  return age;
}
public void setAge(int age) {
  this.age = age;
}
}亲,记得重写 Person类。还有加强for里的!

评分

参与人数 1技术分 +1 黑马币 +3 收起 理由
狼王 + 1 + 3 谢谢你的回复,论坛需要你

查看全部评分

回复 使用道具 举报
Angel♥儱唲 发表于 2013-11-19 17:23
public class ThreadTest5 {
        public static void main(String[] args) {
                List lis ...

高级for循环是用Object类型啊,之后再强转Person类型,我没用泛型啊
回复 使用道具 举报
Angel♥儱唲 发表于 2013-11-19 17:23
public class ThreadTest5 {
        public static void main(String[] args) {
                List lis ...

看见了,唉,还真不容易发现
回复 使用道具 举报
终结者 发表于 2013-11-19 21:46
高级for循环是用Object类型啊,之后再强转Person类型,我没用泛型啊

亲,你看下你的代码
for(Object obj :  list)
{
      Person p=(Person)list;      你这里应该把list改成obj    Person p=(Person)obj
}
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马