国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

Java數組聲明、創建、初始化基礎

2019-11-26 16:16:13
字體:
來源:轉載
供稿:網友

一維數組的聲明方式:

type var[]; 或type[] var;
聲明數組時不能指定其長度(數組中元素的個數),
Java中使用關鍵字new創建數組對象,格式為:
數組名 = new 數組元素的類型 [數組元素的個數]

實例:

TestNew.java:

程序代碼:

public class TestNew {    public static void main(String args[]) {      int[] s ;      int i ;      s = new int[5] ;      for(i = 0 ; i < 5 ; i++) {        s[i] = i ;      }      for(i = 4 ; i >= 0 ; i--) {        System.out.println("" + s[i]) ;      }    }  } 

初始化:

1.動態初始化:數組定義與為數組分配空間和賦值的操作分開進行;
2.靜態初始化:在定義數字的同時就為數組元素分配空間并賦值;
3.默認初始化:數組是引用類型,它的元素相當于類的成員變量,因此數組分配空間后,每個元素也被按照成員變量的規則被隱士初始化。

實例:

TestD.java(動態):

程序代碼: 

public class TestD {    public static void main(String args[]) {      int a[] ;      a = new int[3] ;      a[0] = 0 ;      a[1] = 1 ;      a[2] = 2 ;      Date days[] ;      days = new Date[3] ;      days[0] = new Date(2008,4,5) ;      days[1] = new Date(2008,2,31) ;      days[2] = new Date(2008,4,4) ;    } }  class Date {    int year,month,day ;    Date(int year ,int month ,int day) {      this.year = year ;      this.month = month ;      this.day = day ;    } } 



TestS.java(靜態):

程序代碼:

public class TestS   {      public static void main(String args[]) {        int a[] = {0,1,2} ;        Time times [] = {new Time(19,42,42),new Time(1,23,54),new Time(5,3,2)} ;      }   }    class Time   {      int hour,min,sec ;      Time(int hour ,int min ,int sec) {        this.hour = hour ;        this.min = min ;        this.sec = sec ;      }   }  

TestDefault.java(默認):
程序代碼:

public class TestDefault   {      public static void main(String args[]) {        int a [] = new int [5] ;        System.out.println("" + a[3]) ;      }   }  
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 锡林浩特市| 朔州市| 兴仁县| 舞阳县| 青河县| 黎城县| 韩城市| 松滋市| 新宾| 鱼台县| 赣州市| 颍上县| 进贤县| 阆中市| 佛坪县| 孟村| 若羌县| 朔州市| 扶绥县| 宝兴县| 临桂县| 巨野县| 合水县| 唐河县| 眉山市| 安康市| 湾仔区| 河北区| 农安县| 陆丰市| 绥阳县| 新津县| 镇安县| 东安县| 隆安县| 新干县| 东源县| 越西县| 大连市| 界首市| 临沭县|