国产探花免费观看_亚洲丰满少妇自慰呻吟_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]) ;      }   }  
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 聂拉木县| 巧家县| 济源市| 绥中县| 南部县| 定南县| 宁陵县| 阳朔县| 胶南市| 微博| 象州县| 交城县| 曲阜市| 靖州| 斗六市| 武邑县| 贵州省| 渝中区| 鄢陵县| 梁平县| 乐至县| 兴宁市| 京山县| 壤塘县| 壶关县| 黔江区| 镇宁| 封开县| 松潘县| 论坛| 保德县| 汉中市| 交口县| 南安市| 莲花县| 塘沽区| 隆化县| 闵行区| 石景山区| 始兴县| 林口县|