day05-数组的操作介绍(取值和赋值)
This commit is contained in:
@@ -30,6 +30,13 @@ package com.inmind.array01;
|
||||
|
||||
总结:1.如果要保存的数据,不确定,就应该通过格式一,数组动态初始化这种形式创建
|
||||
2.如果要保存的数据,已经确定,就使用格式二或格式三,推荐使用格式三
|
||||
|
||||
--------------------------------------------------
|
||||
|
||||
- 数组中有索引(门牌号),从0开始递增
|
||||
- 数组中有个length属性,数组.length获取数组固定的长度
|
||||
- 数组取值:int a = 数组[索引]
|
||||
- 数组赋值:数组[索引] = 值
|
||||
*/
|
||||
public class Demo01 {
|
||||
public static void main(String[] args) {
|
||||
@@ -39,7 +46,27 @@ public class Demo01 {
|
||||
//数组定义格式二:静态初始化
|
||||
int[] arr2 = new int[]{1,20,3,4,50};
|
||||
|
||||
//数组定义格式三:静态初始化
|
||||
//数组定义格式三:静态初始化简写形式
|
||||
int[] arr3 = {100,200};
|
||||
|
||||
//通过数组的属性length获取数组的长度
|
||||
System.out.println(arr1.length);//3
|
||||
System.out.println(arr2.length);//5
|
||||
System.out.println(arr3.length);//2
|
||||
|
||||
//扩展:数组的定义格式三,只能在定义数组的时候直接赋值,当数组创建完毕后就不能直接赋值了
|
||||
//arr3 = {300,400};
|
||||
System.out.println("-----------------------------");
|
||||
//定义过后的数组只能通过索引来操作
|
||||
//取值操作
|
||||
int a = arr2[4];
|
||||
System.out.println(a);
|
||||
System.out.println(arr1[1]);//0
|
||||
System.out.println(arr2[4]);//50
|
||||
|
||||
//赋值操作
|
||||
System.out.println(arr2[1]);//20
|
||||
arr2[1] = 2;
|
||||
System.out.println(arr2[1]);//2
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user