(转)java二维数组的深度学习(静态与动态)

时间:2023-03-10 03:02:41
(转)java二维数组的深度学习(静态与动态)

转自:http://developer.51cto.com/art/200906/128274.htm,谢谢

初始化:

1.动态初始化:数组定义与为数组分配空间和赋值的操作分开进行;
2.静态初始化:在定义数字的同时就为数组元素分配空间并赋值;
3.默认初始化:数组是引用类型,它的元素相当于类的成员变量,因此数组分配空间后,每个元素也被按照成员变量的规则被隐士初始化。
实例:

TestD.java(动态):

程序代码:

  1. 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) ;
  12. days[1] = new Date(2008,2,31) ;
  13. days[2] = new Date(2008,4,4) ;
  14. }
  15. }
  16. class Date
  17. {
  18. int year,month,day ;
  19. Date(int year ,int month ,int day) {
  20. this.year = year ;
  21. this.month = month ;
  22. this.day = day ;
  23. }
  24. }

TestS.java(静态):

程序代码:

  1. public class TestS
  2. {
  3. public static void main(String args[]) {
  4. int a[] = {0,1,2} ;
  5. Time times [] = {new Time(19,42,42),new Time(1,23,54),new Time(5,3,2)} ;
  6. }
  7. }
  8. class Time
  9. {
  10. int hour,min,sec ;
  11. Time(int hour ,int min ,int sec) {
  12. this.hour = hour ;
  13. this.min = min ;
  14. this.sec = sec ;
  15. }
  16. }

TestDefault.java(默认):

程序代码:

    1. public class TestDefault
    2. {
    3. public static void main(String args[]) {
    4. int a [] = new int [5] ;
    5. System.out.println("" + a[3]) ;
    6. }
    7. }