一维数组

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值