1 数组的定义
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public static void main(String[] args) { // 数组 --- 存储固定类型,固定长度容器。 // 长度 元素的个数 // 创建数组 -- 语法格式 // 元素类型 [] 数组名 = new 元素类型[长度]; // 长度为5的整型数组 // int [] arr = new int [5]; // 数组 -- array // 元素 -- 数组中存储的数据 elements // 长度 -- 元素的个数 length // 元素类型 [] 数组名 = new 元素类型[] {元素1,元素2,元素3}; // 不能给出长度,元素之间用逗号隔开 // int [] arr = new int[] {2,3,4,5,7};// 不能写长度 // 元素类型 [] 数组名 = {元素1,元素2,元素3}; // int [] arr = {2,3,4,5,7}; }数组的定义
2 数组的基本概念
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public static void main(String[] args) { int [] arr = {2,3,4,5,7}; String arr2 [] = new String[5]; // 数组是引用数据类型 --- 可以赋值为null; // 数组的长度--元素的个数 --- 数组名.length System.out.println(arr.length);// 直接输出 int len = arr.length;// 拿到长度赋值给一个变量。 System.out.println(len); // 索引 index 数组是有序的。有索引,-- 编号,位置。从0 开始 // 第一个元素的索引值就是 0 ;依次类推。 // 索引,index,有时候也叫角标,下标。编号 // 最后一个元素的索引。--- 长度-1;arr.length - 1 // 数组元素的访问。元素-- 数组中的数据。 // 数组名[索引] --- 指定索引处的元素。 // arr[0] ---- 数组arr的0索引处的元素 // 数组元素的访问1 --- 直接取出元素,输出,赋值 int e1 = arr[0];// 取出元素赋值给e1; System.out.println(e1); System.out.println(arr[0]);//直接输出 // 数组元素的访问2 --- 给指定索引处赋值, arr[0] = 99;// 改,给arr数组0索引处从新赋值为 99 String arr2 [] = new String[5]; System.out.println(arr2[0]);// 输出0索引处的元素。 // 有默认值。如果元素是整形,默认是0 // 浮点型 0.0 // 布尔 false // String -- null }数组的基本概念
3 数组的常见错误,下标越界和空指针异常
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public static void main(String[] args) { 2 // int [] arr = {2,3,4,5,7}; 3 //// 数组是引用数据类型,可以赋值为null 4 // arr = null; 5 // // 引用数据类型的变量为null的时候,表示空。不指向任何数据。 6 //// 如果为null;你还拿着它使用的话,就会报空指针异常。 7 // 8 // System.out.println(arr.length); 9 // Exception in thread "main" java.lang.NullPointerException: Cannot read the array length because "arr" is null 10 // at com.cy.arr.Demo3_Array.main(Demo3_Array.java:11) 11 12 13 // java.lang.NullPointerException --- 包名加类名的形式。--- 全类名 14 // --- 见到空指针异常的时候。往引用数据类型为null 啦 15 16 // int [] arr = {2,3,4,5,7}; 17 // 索引越界的问题--- 索引的取值范围-- 0 -- arr.length-1 只要不在这个范围,就会索引越界 18 19 // System.out.println(arr[-1]); 20 // Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 5 21 // at com.cy.arr.Demo3_Array.main(Demo3_Array.java:22) 22 23 // java.lang.ArrayIndexOutOfBoundsException -- 数组索引越界异常 24 25 26 // [0,5)--- 包前不包后 27 // 只要报索引越界异常,就往索引取值范围--不在0到长度减一范围内 28数组的常见错误
4数组的遍历迭代
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class xp implements IntConsumer{ 2 3 @Override 4 public void accept(int value) { 5 6 System.out.println(value); 7 } 8 9 } 10 public class Demo4_Array { 11 public static void main(String[] args) { 12 // 遍历迭代--- 依次取出容器中元素。 13 int [] arr = {1,2,3,4,5}; 14 15 // System.out.println(arr[0]); 16 // System.out.println(arr[1]); 17 // System.out.println(arr[2]); 18 // System.out.println(arr[3]); 19 // System.out.println(arr[4]); 20 // 用循环改进 21 // for(int i = 0;i<arr.length;i++){ 22 // System.out.println(arr[i]);// 0--arr.length-1 23 // } 24 25 // for(元素类型 表示元素的变量 :数组名) { 26 // // 数组有多少个元素,大括号就会进来多少次。 27 // } 28 // for(int e :arr ) { 29 // // 第一次进入大括号,e表示的就是第一个元素。0位置的元素 30 // System.out.println(e); 31 // } 32 33 // stream 流的方式 34 35 // Arrays.stream(arr).forEach(new xp()); 36 // Arrays.stream(arr).forEach(new IntConsumer() { 37 // 38 // @Override 39 // public void accept(int value) { 40 // System.out.println(value); 41 // 42 // } 43 // }); 44 // Arrays.stream(arr).forEach((int value)-> { 45 // System.out.println(value); 46 // 47 // 48 // }); 49 50 51 // Arrays.stream(arr).forEach(( value)-> { 52 // System.out.println(value); 53 // 54 // 55 // }); 56 57 Arrays.stream(arr).forEach(value -> System.out.println(value)); 58 59 60 61 62 63 }数组遍历迭代三种方式
5 求数组最大值
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public static void main(String[] args) { 2 // 求数组最大值 3 int [] arr = {11,22,3,44,5,6}; 4 // 思路,默认第一个元素最大。参照物。 5 // 后面依次比较,如果后面的元素大于参照物,就保留 6 // 到最后,参照物保留的都是最大 7 int max = arr[0]; 8 for(int i = 1;i<arr.length;i++) { 9 if(arr[i]>max) { 10 max = arr[i];// 从新给max赋值,赋值为当前的元素。arr[i] 11 } 12 } 13 // 循环结束,max记录的就是最大 14 System.out.println(max); 15 }数组最大值
6 数组的查找-普通查找
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public static void main(String[] args) { 2 int [] arr = {11,22,3,44,5,6}; 3 int index = -1; 4 // 查找,存在就输出索引,否则输出 -1 5 int key = 44; 6 for(int i = 0;i<arr.length-1;i++) { 7 if(arr[i] == key) { 8 index = i ;// i表示的是索引 9 break;// 找到之后就停止 10 } 11 } 12 System.out.println(index); 13 } 14 15 public static int searchElementArray(int []arr,int key) { 16 for(int i = 0;i<arr.length;i++) { 17 if(arr[i] == key) { 18 return i; 19 } 20 } 21 return - 1; 22 }数组查找
标签:2022122,arr,Java,int,元素,数组,println,JAVA,out From: https://www.cnblogs.com/haizinihao/p/16944419.html