已知动态数组(ArrayList)有一个student对象但是学生对象里面的score属性没有值,请将student对象的score属性赋值,然后从动态数组里面取出学生对象,打印学生对象 我实在是搞不弄这个题的意义,按我的写发就是以下代码,似乎太简单了import java.util.*;public class Student {
String name = "张三";
String sex = "男";
int id = 1;
int score;
}
public class Arr { public static void main(String[] args) {
Student student = new Student();
ArrayList arr = new ArrayList();
arr.add(student);//ArrayList中存有一个没有score的Student的类对象

Student s = (Student) arr.get(0);//得到这个对象
s.score = 90; //给score赋值

System.out.println("学号:"+s.id);
System.out.println("姓名:"+s.name);
System.out.println("性别:"+s.sex);
System.out.println("成绩:"+s.score); }}

解决方案 »

  1.   

    已知动态数组(ArrayList)有一个student对象但是学生对象里面的score属性没有值,请将student对象的score属性赋值,然后从动态数组里面取出学生对象,打印学生对象  
      

  2.   

    我估计是考你类型判断
    package test;import java.util.ArrayList;public class StudentTest
    {
    public static void main(String[] args)
    {
    ArrayList<Object> arr=new ArrayList<Object>();
    Student stu=new Student();
    stu.setName("studentA");
    stu.setId(1);
    arr.add("wuxiao0313");
    arr.add(stu);
    arr.add(new Integer(222));
    for(Object o : arr)

    if(o.getClass().getName().equals("test.Student"))

     ((Student)o).setScore(100);
    System.out.println(o.toString());
    }
    }
    }
    }
    class Student
    {
    String name,sex;
    int id,score;
    public int getId()
    {
    return id;
    }
    public void setId(int id)
    {
    this.id = id;
    }
    public String getName()
    {
    return name;
    }
    public void setName(String name)
    {
    this.name = name;
    }
    public int getScore()
    {
    return score;
    }
    public void setScore(int score)
    {
    this.score = score;
    }
    public String getSex()
    {
    return sex;
    }
    public void setSex(String sex)
    {
    this.sex = sex;
    }
    @Override
    public String toString()
    {
    return "名字为:"+this.getName()+"成绩为:"+this.getScore();
    }
    }
      

  3.   

    同意楼上的,问题在于你要从List里面找到 Student 对象。建议如下判断语句
    if(o.getClass().getName().equals("test.Student")) 可以改为
    if(o instanceof Student) 比较好!
      

  4.   

    是不是score为String类型更好一些?if (((Student)o).getScore != null)
      ....
      

  5.   

    关键是,得到Student在容器中的引用
      

  6.   

    应该就是考察容器的使用,1.4之前放入容器的对象以Object类型存取,会失去具体的类型信息,取出后需要自己转型。出于严谨,instanceof 是应该加上的
    不过,一般一个list内应该存放同类型的对象,毕竟容器只是容器,只担任数据存取结构的功能;如果什么都放,除非是必须的,不然这样设计上就有点问题了
      

  7.   

    public   class   Student   { public 去掉,一个类里只能有一个public,基础知识要补一下了。,
      

  8.   

    10楼
    应该是一个文件里只能有一个public类