//下面的代码创建一个三个元素的引用类型数组,学习对象的时候说new会产生新的对象
//想知道的是下面代码给数组赋初值用了三个new 是不是同时产生了三个对象?
public class TestD
2. {
3. public static void main(String args[]) {
4. int a[] ;
5. a = new int[3] ;
6. a[0] = 0 ;
7. a[1] = 1 ;
8. a[2] = 2 ;
9. Date days[] ;
10. days = new Date[3] ;
11. days[0] = new Date(2008,4,5) ; //不是说new会产生新对象么?
12. days[1] = new Date(2008,2,31) ;
13. days[2] = new Date(2008,4,4) ;
14. }
15. }
16.
17. class Date
18. {
19. int year,month,day ;
20. Date(int year ,int month ,int day) {
21. this.year = year ;
22. this.month = month ;
23. this.day = day ;
24. }
25. }
26.
------解决思路----------------------
Date days[] ; 这里有一个引用,指向了下面的new Date[3]
10. days = new Date[3] ;
11. days[0] = new Date(2008,4,5) ; 依次产生三个对象
12. days[1] = new Date(2008,2,31) ;
13. days[2] = new Date(2008,4,4) ;