(studentElement)arraylist.get(0);

解决方案 »

  1.   

    你要告诉编译器你的stu2是StudentElement型的阿。
    用((StudentElement)stu2).name就行了。
    这叫做cast
      

  2.   

    楼上的说得对,arraylist里边放的是Object从里边get出来以后需要进行一下类型转换
      

  3.   

    ArrayList里边每一个元素都是一个对象.所以每得到一个元素之前强制转换成对象或类.
      

  4.   

    同意 kindlemoney(钱老大) 的说法。类似的还有hashmap等等
      

  5.   

    java语言所有的类都是Object的子类,这是java语言规定的,是默认的.
      

  6.   

    import java.util.*;
    class studentElement
    {
    String name;
    int score;
    }
    public class test
    {
    public static void main(String[] args)
    {
    ArrayList arraylist=new ArrayList();
    studentElement stu=new studentElement();
    Object stu2=new Object();
                      String a;
    stu.name="monkey";
    stu.score=90;
    arraylist.add(stu);
    stu2=arraylist.get(0);
    a=((studentElement)stu2).name;
    System.out.println(a);
    }
    }
    这样做,符合要求,不报错