2 bizhen npu bizhen_npu 于 2016.01.31 18:53 提问

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
Evankaka   Ds   Rxr 2016.01.31 18:59
已采纳

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

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!