bizhen_npu
2016-01-31 10:53
采纳率: 50%
浏览 1.4k
已采纳

java关于ArrayList()的简单问题

package Hope;
import java.util.ArrayList;
import java.util.List;
public class ListTest {
public List coursesToSelec;

/* public ListTest(){
this.coursesToSelect = new ArrayList();
}
*/
coursesToSelect = new ArrayList(); //把上面的内容注释掉,换成这段代码为什么不行?

public void addCourse(){
    Course cr1 = new Course("离散数学",1);
    coursesToSelect.add(cr1);
    Course temp = (Course) coursesToSelect.get(0);
    System.out.println(temp.name+" "+temp.number);

}      

public static void main(String[] args){
    ListTest a = new ListTest();
    a.addCourse();
}

}

package Hope;
public class Course {
String name;
int number;
public Course(String name,int number){
this.name = name;
this.number = number;
}

}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • Evankaka 2016-01-31 10:59
    已采纳

    /* public ListTest(){
    this.coursesToSelect = new ArrayList();
    }
    */
    换成这样子么?如果是下面这样子,是可以的啊
    public List coursesToSelec = = new ArrayList();

    点赞 评论

相关推荐 更多相似问题