Lesson Three 2018-04-17 20:57:37
1.数组是多个 相同类型 数据的组合,
2.数组中的元素可以是任何数据类型,包括基本数据类型和引用数据类型。3.数组属于引用类型,和数组型数据是对象,数组中的每个元素相当于该对象的成员变量。4.数组一旦初始化,其长度是不可变的。
1.如何定义一个数组 1.1数组的声明 String names[]; int scores[];
1.2初始化 1.2.1 静态初始化:初始化数组与给数组元素赋值 同时 进行 names = new String[]{"zhangsan","lisi","wanger"}; 1.2.2动态初始化:初始化数组与给数组元素赋值 分开 进行 scores = new int[4];
不管是动态还是静态初始化数组,一定在创建的时候,就指明了数组的长度。
2.如何调用 相应的数组元素:通过数组元素的下标方式来调用。 下标从0开始,到n-1结束。n代表数组长度。 scores[0] = 77; scores[1] = 88; scores[2] = 99; scores[3] = 100;
3.数组的长度:通过数组的length属性。 System.out.println(names.length); //3 System.out.println(scores.length); //4
4.如何遍历数组元素。 for (int j = 0; j < scores.length; j++) { System.out.println(scores[j]);
基本数据类型: byte short int long float double char bool
1.于 byte short int long 而言, 创建数组后,默认值为0 int scores[] = new int[4]; scores[0] = 77; scores[3] = 77; for (int i1 = 0; i1 < scores.length; i1++) { System.out.println(scores[i1]); }
2.于 float 和double 而言,默认为0.0 float f[] = new float[3]; f[0] = 1.2f;
for (int i2 = 0; i2 < f.length; i2++) {
System.out.println(f[i2]); }
3.于char 而言默认为空格 char c[] = new char[3]; for (int i3 = 0; i3 < c.length; i3++) { System.out.println(c[i3]); }
4.对于bool而言,默认为false
boolean b[] = new boolean[4]; for (int i4 = 0; i4 < b.length; i4++) { System.out.println(b[i4]);}
5.对于引用类型,默认为null 以String为例
String strs[] = new String [4]; strs[0] = "11"; strs[1] = "22"; strs[3] = "33"; for (int i = 0; i < strs.length; i++) { System.out.println(strs[i]); }
转载于:https://www.cnblogs.com/Fkuennhvo/p/8869992.html