Java基础学习(四)基本语法3之一维数组

数组:相同数据类型的数据的组合。

【如】 int score1 = 72;

int score2 = 90;

int score3 = 59;

使用数组:

1.数组的初始化

int[] scores1 = new int[]{72,90,59};//静态初始化:在声明并初始化数组与给数组相应的元素赋值操

同时进行。

int scores2[] = new int[3];//动态初始化:在声明并初始化数组与给数组相应的元素赋值操作分开

行。

scores2[0] = 72;

scores2[1] = 90;

scores2[2] = 59;

//声明数组的错误写法:

1)String[] names = new String[5]{"AA","BB","CC"};

2)int i[10];

3)int i = new int[];

注:不管是动态还是静态初始化数组,一定在创建的时候,就指明了数组的长度!

2.如何引用数组元素:通过数组的下角标的方式。下角标从0开始,到n-1结束。其中n为数组的长度。

3.数组的长度:通过数组的属性length来调用。

System.out.println(scores2.length);//3

4.如何遍历数组

for(int i = 0;i < scores1.length;i++){

System.out.println(scores1[i]);

}

5.关于数组元素的默认初始化值

1)byte short int long 而言:0

2)float double 而言:0.0

3)char而言:空格

4)boolean而言:false

5)引用类型变量而言:null

6.数组的内存结构

Java基础学习(四)基本语法3之一维数组

对于数组来讲:

Java基础学习(四)基本语法3之一维数组

weinxin
我的微信
有问题微信找我
DannyWu

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

Protected with IP Blacklist CloudIP Blacklist Cloud