Java中一維二維數(shù)組的靜態(tài)和動(dòng)態(tài)初始化
今天我們要開始來講講Java中的數(shù)組,包括一維數(shù)組和二維數(shù)組的靜態(tài)初始化和動(dòng)態(tài)初始化
數(shù)組概述:
數(shù)組可以看成是多個(gè)相同類型數(shù)據(jù)的組合,對(duì)這些數(shù)據(jù)的統(tǒng)一管理;
數(shù)組變量屬于引用數(shù)據(jù)類型,數(shù)組也可以看成是對(duì)象,數(shù)組中的每一個(gè)元素相當(dāng)于該對(duì)象的成員變量;
數(shù)組中的元素可以是任何數(shù)據(jù)類型,包括基本數(shù)據(jù)類型和引用數(shù)據(jù)類型;
一維數(shù)組的聲明:
聲明方式: 例如; int a [ ] = new int [3];
Java語言中 聲明是不能指定其長度[數(shù)組中元素的個(gè)數(shù)];
非法聲明; int a [5];
數(shù)組對(duì)象的創(chuàng)建:
public class Test {
public static void main (String args[ ] ) {
int [ ] s;
s = new int [5];
for(int i = 0; i < 5; i ++) {
s[i] = 2 * i + 1;
}
}
}
一維數(shù)組初始化
動(dòng)態(tài)初始化:
public class Test {
public static void main (String args [ ] ) {
int a [ ];
a = new int [3];
//int a [ ] = {1,2,3};
Date days [ ];
days = new Date [3];
days [0] = new Date(1,4,20040);
days [1] = new Date(2,4,20040);
days [2] = new Date(3,4,20040);
}
}
class Date {
int year,month,day;
Date (int y,int m,int d) {
year = y; month = m; day = d;
}
}
靜態(tài)初始化
public class Test {
public static void mian (String args [ ] ) {
int a[ ] = new int [ ] {3,9,8};
Date days[ ] = {
new Date(1,4,2004),
new Date(2,4,2004),
new Date(3,4,2004)
};
}
}
class Date {
int year,month,day;
Date(int y,int m,int d) {
year = y; month = m;day = d;
}
}
二維數(shù)組
二維數(shù)組可以看成數(shù)組為元素的數(shù)組,例如:
int a [ ][ ] = {{1,2},{3,4,5,6},{7,8,9}};
二維數(shù)組初始化
靜態(tài)初始化:
int intA [ ] [ ] = {{1,2},{2,3},{3,4,5}};
int intB [ 3] [ 2] = {{1,2},{2,4},{4,5}}; 非法
動(dòng)態(tài)初始化:
int a [ ] [ ] = new int [3] [5]; int b [ ] [ ] = new int [3] [ ]; b[0] = new int [2]; b[1] = new int [3]; b[2] = new int [5];
相關(guān)文章
Spring 應(yīng)用中集成 Apache Shiro的方法
這篇文章主要介紹了Spring 應(yīng)用中集成 Apache Shiro的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05
SpringBoot實(shí)現(xiàn)異步事件Event詳解
這篇文章主要介紹了SpringBoot實(shí)現(xiàn)異步事件Event詳解,異步事件的模式,通常將一些非主要的業(yè)務(wù)放在監(jiān)聽器中執(zhí)行,因?yàn)楸O(jiān)聽器中存在失敗的風(fēng)險(xiǎn),所以使用的時(shí)候需要注意,需要的朋友可以參考下2023-11-11
JAVA實(shí)現(xiàn)經(jīng)典掃雷游戲的示例代碼
windows自帶的游戲《掃雷》是陪伴了無數(shù)人的經(jīng)典游戲,本程序參考《掃雷》的規(guī)則進(jìn)行了簡化,用java語言實(shí)現(xiàn),采用了swing技術(shù)進(jìn)行了界面化處理。感興趣的可以學(xué)習(xí)一下2022-01-01
java數(shù)據(jù)結(jié)構(gòu)實(shí)現(xiàn)機(jī)器人行走
這篇文章主要為大家詳細(xì)介紹了java數(shù)據(jù)結(jié)構(gòu)實(shí)現(xiàn)機(jī)器人行走,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
SpringBoot導(dǎo)出Excel表格到指定路徑的代碼詳解
Spring Boot導(dǎo)出Excel通常涉及到使用第三方庫如Apache POI或者XlsxWriter等,它們能幫助你在Spring應(yīng)用中生成并下載Excel文件,那么SpringBoot如何導(dǎo)出Excel表格到指定路徑,本文將給大家詳細(xì)的介紹一下2024-07-07
在Eclipse中運(yùn)行Solr 基礎(chǔ)知識(shí)
Solr我還是個(gè)菜鳥,寫這一些文章只是記錄一下最近一段時(shí)間學(xué)習(xí)Solr的心得,望各位同仁不要見笑,還希望多多指點(diǎn)2012-11-11
SpringSecurity數(shù)據(jù)庫進(jìn)行認(rèn)證和授權(quán)的使用
本文主要介紹了用戶的賬號(hào)、密碼以及角色信息在數(shù)據(jù)庫中的認(rèn)證和授權(quán),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08

