数组
数组array是用来表示一组数据的集合或者一系列的数据。
声明数组 int a[] = {1 ,2 , 3 ,9}; 这种声明的过程又叫作初始化
把数组想象成一个框,但是这个框只能装同一种类型的变量。定义数组之后,数组内的类型统一,不可改变。
举例:声明一个4个整数的数组,输出a[3]
1 package com.feimao.array; 2 3 public class Array01 { 4 public static void main(String args[]){ 5 int a[] = {1 , 3 , 5 , 10}; 6 System.out.println(a[3]); 7 } 8 }
数据的角标是从0开始的。
举例:声明一个数组{9 , 3 , 7 , 6},求该数组的各个数的和,并且求其平均数
1 package com.feimao.array; 2 3 4 5 public class Array02 { 6 7 public static void main(String args[]){ 8 9 int a[] = {9 , 3 , 7 ,6}; 10 11 int sum = 0; 12 13 for(int i = 0 ; i < a.length ; i++){ 14 15 sum = sum + a[i]; 16 17 } 18 19 double avg = sum / (double)a.length; 20 21 System.out.println(avg); 22 23 } 24 25 }
- length 表示数组的长度,其实就是4
举例:收发红包,用一个数组表示每天的收发红包数,最后N天求红包之和
1 package com.feimao.array; 2 3 4 5 public class Array05 { 6 7 public static void main(String args[]){ 8 9 int lucky[] = {3, 5, -2, -6, 4, 0, 8}; 10 11 int cumlative[] = new int[lucky.length]; 12 13 cumlative[0] = lucky[0]; 14 15 for (int i = 1; i < cumlative.length; i++) { 16 17 cumlative[i] = cumlative[i-1] + lucky[i]; 18 19 } 20 21 for (int i = 0; i <= cumlative.length; i++) { 22 23 System.out.println(cumlative[i]); 24 25 } 26 27 } 28 29 }
举例:Java数组排序
1 package com.com.zhubaobao.code; 2 3 public class ArrayRefDemo { 4 public static void main(String args[]) { 5 int score[] = {76, 44, 40, 39, 80, 100, 12, 8}; 6 int age[] = {87, 23, 98, 34, 18, 9, 29, 19}; 7 sort(score); 8 print(score); 9 System.out.println(" ------------------------------------"); 10 sort(age); 11 print(age); 12 } 13 14 public static void sort(int temp[]) { 15 for (int i = 0; i < temp.length; i++) { 16 for (int j = 0; j < temp.length; j++) { 17 if (temp[i] < temp[j]) { 18 int x = temp[i]; 19 temp[i] = temp[j]; 20 temp[j] = x; 21 } 22 } 23 } 24 } 25 26 public static void print(int temp[]) { 27 for (int i = 0; i < temp.length; i++) { 28 System.out.print(temp[i] + " "); 29 } 30 } 31 }
对于排序操作,Java中也有类库支持的,可以直接使用“java.util.Arrays.sort()”对数组进行排序。
举例:Java类库完成数组排序
1 package com.com.zhubaobao.code; 2 3 public class ArrayRefDemo01 { 4 public static void main(String args[]) { 5 int score[] = {76, 44, 40, 39, 80, 100, 12, 8}; 6 int age[] = {87, 23, 98, 34, 18, 9, 29, 19}; 7 /*sort(score);*/ 8 java.util.Arrays.sort(score); 9 print(score); 10 System.out.println(" ------------------------------------"); 11 /*sort(age);*/ 12 java.util.Arrays.sort(age); 13 print(age); 14 } 15 16 /* public static void sort(int temp[]) { 17 for (int i = 0; i < temp.length; i++) { 18 for (int j = 0; j < temp.length; j++) { 19 if (temp[i] < temp[j]) { 20 int x = temp[i]; 21 temp[i] = temp[j]; 22 temp[j] = x; 23 } 24 } 25 } 26 }*/ 27 28 public static void print(int temp[]) { 29 for (int i = 0; i < temp.length; i++) { 30 System.out.print(temp[i] + " "); 31 } 32 } 33 }
数组最常见的面试?
1.什么是数组的扩容,扩容用到什么方法?
1)Java数组对象的大小是固定不变的,数组对象是不可扩容的。
2)利用数组复制方法能够变通的实现数组扩容。
3)System.arraycopy()能够复制数组。
4)Arrays.copyOf()能够简便的创建数组副本。
5)创建数组副本的同一时候将数组长度添加就变通的实现了数组的扩容。
System.arraycopy(a1 , 3 , a2 , 1 , 3);
copy数组参数含义:a1为源数组 ,3为a1数组的开始点,数组的下标是从[0]开始的,所以是第四个数字。
a2为目标数组,替换的位置为[1],也就是第二个数字。最后的3为替换的数组复制长度。
举例:数组扩容
1 package com.com.zhubaobao.code; 2 3 import java.util.Arrays; 4 5 /** 数组变长算法! 6 * 数组对象长度不可改变 7 * 可是非常多实际应用须要长度可变的数组 8 * 能够採用复制为容量更大的新数组, 替换原数组, 实现变长操作 9 * */ 10 public class ArrayCopy01 { 11 public static void main(String[] args) { 12 //数组变长(扩容)算法! 13 int a1[] = {1 , 2 , 3}; 14 a1 = Arrays.copyOf(a1 , a1.length+1); 15 a1[a1.length-1] = 4; 16 System.out.println(Arrays.toString(a1)); 17 //字符串连接原理 18 char[] chs = { '中', '国' }; 19 chs = Arrays.copyOf(chs, chs.length + 1); 20 chs[chs.length - 1] = '北'; 21 chs = Arrays.copyOf(chs, chs.length + 1); 22 chs[chs.length - 1] = '京'; 23 //字符数组依照字符串打印 24 System.out.println(chs);//中国北京 25 //其它数组依照对象打印 26 System.out.println(a1);//[I@4f1d0d 27 } 28 }