• <th id="kadjp"></th>

            1. ?
                開發(fā)技術(shù) / Technology

                Java數(shù)組聲明、創(chuàng)建、初始化

                日期:2015年3月31日  作者:zhjw  來源:互聯(lián)網(wǎng)    點(diǎn)擊:822

                一維數(shù)組的聲明方式:
                type var[]; 或type[] var;

                聲明數(shù)組時不能指定其長度(數(shù)組中元素的個數(shù)),

                Java中使用關(guān)鍵字new創(chuàng)建數(shù)組對象,格式為:
                數(shù)組名 = new 數(shù)組元素的類型 [數(shù)組元素的個數(shù)]

                實(shí)例:
                TestNew.java:

                程序代碼:

                
                 
                1. public class TestNew  
                2. {  
                3.      public static void main(String args[]) {  
                4.          int[] s ;  
                5.          int i ;  
                6.          s = new int[5] ;  
                7.          for(i = 0 ; i < 5 ; i++) {  
                8.              s[i] = i ;  
                9.          }  
                10.          for(i = 4 ; i >= 0 ; i--) {  
                11.              System.out.println("" + s[i]) ;  
                12.          }  
                13.      }   


                初始化:

                1.動態(tài)初始化:數(shù)組定義與為數(shù)組分配空間和賦值的操作分開進(jìn)行;
                2.靜態(tài)初始化:在定義數(shù)字的同時就為數(shù)組元素分配空間并賦值;
                3.默認(rèn)初始化:數(shù)組是引用類型,它的元素相當(dāng)于類的成員變量,因此數(shù)組分配空間后,每個元素也被按照成員變量的規(guī)則被隱士初始化。
                實(shí)例:


                TestD.java(動態(tài)):

                程序代碼:

                
                 
                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.  
                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.  


                TestS.java(靜態(tài)):

                程序代碼:

                
                 
                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.  
                9. class Time     
                10. {     
                11.      int hour,min,sec ;     
                12.      Time(int hour ,int min ,int sec) {     
                13.          this.hour = hour ;     
                14.          this.min = min ;     
                15.          this.sec = sec ;     
                16.      }     
                17. }    


                TestDefault.java(默認(rèn)):

                程序代碼:

                
                 
                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. }   

                国产欧美在线观看,国产精品白浆冒出视频,91精品国产91热久久久福利,大伊香蕉在线精品视频97 国产精品美女久久福利 国产精品黄的免费观看
              • <th id="kadjp"></th>