更新時間:2020-10-20 17:53:36 來源:動力節(jié)點 瀏覽1572次
數(shù)組(Array)是有序的元素序列。在程序中可以使用下標變量,即說明這些變量的整體為數(shù)組,數(shù)組中的每個變量的數(shù)據(jù)類型是相同的。當數(shù)組中每個元素都只帶有一個下標時,稱這樣的數(shù)組為一維數(shù)組。一維數(shù)組是數(shù)組里面最重要的基本組成部分,本文我們就一起來全面解析一下一維數(shù)組。
1.一維數(shù)組聲明方式
方式一
數(shù)據(jù)類型 數(shù)組名[] = null ; //聲明一維數(shù)組
數(shù)組名 = new 數(shù)組類型[長度]; // 分配內(nèi)存給數(shù)組
方式二
數(shù)據(jù)類型[] 數(shù)組名 = null ; //聲明一維數(shù)組
數(shù)組名 = new 數(shù)組類型[長度]; // 分配內(nèi)存給數(shù)組
簡寫方式
數(shù)據(jù)類型 數(shù)組名[] = new 數(shù)據(jù)類型[個數(shù)]; //聲明數(shù)組的同時分配內(nèi)存
2.一維數(shù)組中元素的表示方法
數(shù)組的聲明以及簡單輸出
package com.shxt.demo01;
public class ArrayDemo01 {
public static void main(String[] args) {
int[] score = null; //聲明數(shù)組,但是為開辟內(nèi)存空間
score = new int[3]; //為數(shù)組開辟"堆內(nèi)存"空間
System.out.println("score[0]="+score[0]); //分別輸出數(shù)組的每個元素
System.out.println("score[1]="+score[1]); //分別輸出數(shù)組的每個元素
System.out.println("score[2]="+score[2]); //分別輸出數(shù)組的每個元素
// 使用循環(huán)依次輸出數(shù)組中的全部內(nèi)容
for (int i = 0; i < 3; i++) {
System.out.println("score["+i+"]="+score[i]);
}
}
}
對于數(shù)組的訪問采用"數(shù)組名稱[索引或者下標]"的方式,索引從0開始計數(shù),假設程序中取出的內(nèi)容超過了這個下標范圍,例如:score[3]程序運行會存在以下的異常錯誤提示信息:
java.lang.ArrayIndexOutOfBoundsException:3
提示的內(nèi)容為數(shù)組索引超出綁定的異常(經(jīng)常說的數(shù)組越界異常),這個是未來你們初學者經(jīng)常出現(xiàn)的問題,請引起重視.此外,我們發(fā)現(xiàn)以上的程序運行的結果的內(nèi)容都是"0",這是因為聲明的數(shù)組是整型數(shù)組.
默認初始化數(shù)據(jù):數(shù)組元素相當于對象的成員變量,默認值跟成員的規(guī)則是一樣的,重點記憶!
系統(tǒng)將按照如下規(guī)則分配初識值:
數(shù)組元素的類型是基本類型中的整數(shù)類型(byte,short,int和long),則數(shù)組元素的值為0
數(shù)組元素的類型是基本類型中的浮點類型(float,double),則數(shù)組元素的值為0.0
數(shù)組元素的類型是基本類型中的字符類型(char),則數(shù)組元素的值為'\u0000'(空格)
數(shù)組元素的類型是基本類型中的布爾類型(boolean),則數(shù)組元素的值為false
數(shù)組元素的類型是引用數(shù)據(jù)類型(類,接口和數(shù)組),則數(shù)組元素的值為null
為數(shù)組中的元素賦值并進行輸出
聲明整型數(shù)組,長度為5,通過for循環(huán)賦值1,3,5,7,9的數(shù)據(jù)
package com.shxt.demo01;
public class ArrayDemo02 {
public static void main(String[] args) {
int[] score = null; //聲明數(shù)組,但是為開辟內(nèi)存空間
score = new int[5]; //為數(shù)組開辟"堆內(nèi)存"空間
for (int i = 0; i < 5; i++) {
score[i] = i*2+1;
}
for (int i = 0; i < 5; i++) {
System.out.println("score["+i+"]="+score[i]);
}
}
}
數(shù)組長度的取得
數(shù)組名稱.length --> 返回一個int型的數(shù)據(jù)
package com.shxt.demo01;
public class ArrayDemo03 {
public static void main(String[] args) {
int[] score = new int[5];
System.out.println("數(shù)組長度為:"+score.length);
}
}
3. 一維數(shù)組的初始化方式
動態(tài)初始化
之前練習的就是使用的動態(tài)初始化方式
package com.shxt.demo01;
public class ArrayDemo04 {
public static void main(String[] args) {
int[] score = null; //聲明數(shù)組,但是為開辟內(nèi)存空間
score = new int[3]; //為數(shù)組開辟"堆內(nèi)存"空間
score[0] = 100;
score[1] = 200;
score[2] = 300;
}
}
靜態(tài)初始化
數(shù)據(jù)類型[] 數(shù)組名={初始值0,初始值1,...,初始值N}
或者
數(shù)據(jù)類型[] 數(shù)組名 = new 數(shù)據(jù)類型[]{初始值0,初始值1,...,初始值N}
package com.shxt.demo01;
public class ArrayDemo04 {
public static void main(String[] args) {
int[] score = {10,20,30,40,50};
for (int i = 0; i < score.length; i++) {
System.out.println("score["+i+"]="+score[i]);
}
}
}
4一維數(shù)組一定要初始化嗎
在之前我們說在使用Java數(shù)組之前必須要先初始化數(shù)組(即在使用數(shù)組之前,必須先創(chuàng)建數(shù)組).實際上,如果真正掌握了Java數(shù)組中的分配機制,那么可以完全換一個方式來初始化數(shù)組.
始終記住:Java的數(shù)組變量只是引用類型的變量,它并不是數(shù)組對象本身,只是讓數(shù)組變量指向有效的數(shù)組對象,程序中即可使用該數(shù)組變量
package com.shxt.demo01;
public class ArrayDemo08 {
public static void main(String[] args) {
// 靜態(tài)初始化:定義并且初始化nums數(shù)組
int nums[] = {13,34,57,100};
// 定義一個scores數(shù)組變量
int[] scores;
//讓scores數(shù)組執(zhí)行nums所引用的數(shù)組
scores = nums ;
// 對scores進行變量
System.out.print("scores數(shù)組數(shù)據(jù):");
for (int i = 0; i < scores.length; i++) {
System.out.print(scores[i]+"\t");
}
// 將scores數(shù)組中第3個元素賦值為200
scores[2] = 200;
// 訪問 nums 數(shù)組的第3個元素,將看到輸出值為200
System.out.println("\nnums 數(shù)組的第3個元素,將看到輸出值為:"+nums[2]);
}
}
既然數(shù)組內(nèi)容可以進行引用傳遞,那么就可以把數(shù)組作為方法中的參數(shù),而如果一個方法想接收參數(shù),則對應的參數(shù)類型必須是數(shù)組
使用方法接收數(shù)組
package com.shxt.demo01;
public class ArrayDemo09 {
public static void main(String[] args) {
// 靜態(tài)初始化:定義并且初始化nums數(shù)組
int nums[] = {13,34,57,100};
// 引用傳遞, int[] temp = nums
print(nums);
}
public static void print(int[] temp){
for (int i = 0; i < temp.length; i++) {
System.out.print(temp[i]+"\t");
}
}
}
使用方法修改數(shù)組的內(nèi)容
package com.shxt.demo01;
public class ArrayDemo10 {
public static void main(String[] args) {
// 靜態(tài)初始化:定義并且初始化nums數(shù)組
int nums[] = {1,2,3,4,5};
// 引用傳遞, int[] temp = nums
inc(nums); //擴容操作
print(nums); //打印內(nèi)容
}
public static void inc(int[] temp){
for (int i = 0; i < temp.length; i++) {
temp[i]*=2;
}
}
public static void print(int[] temp){
for (int i = 0; i < temp.length; i++) {
System.out.print(temp[i]+"\t");
}
}
}
一維數(shù)組是由數(shù)字組成的以單純的排序結構排列的結構單一的數(shù)組。一維數(shù)組是計算機程序中最基本的數(shù)組。二維及多維數(shù)組可以看作是一維數(shù)組的多次疊加產(chǎn)生的。想要學習數(shù)組,一維數(shù)組是學習二維及多維數(shù)組的基礎,本站的Java基礎教程里面有最新的教程可供參考學習,為眾多Java初學者排憂解難。