#### 数组的定义与使用

• 方式一：`int[] arr;`
• 方式二：`int arr[];`

``````// 初始化方式一
int[] arr = new int[5];
// 初始化方式二
int[] arr2 = new int[]{1, 2, 3, 4, 5};
// 初始化方式二的延伸版，可省略 new int[] 直接赋值
int[] arr3 = {1, 2, 3, 4, 5};``````

#### 数组遍历

``````Integer[] arr = {2, 3, 6, 7, 9};
// 方式一：传统 for
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}``````

``````Integer[] arr = {2, 3, 6, 7, 9};
// 方式二：for each
for (int i : arr) {
System.out.println(i);
}``````

``````Integer[] arr = {2, 3, 6, 7, 9};
// 方式三：jdk 8 Lambda
Arrays.asList(arr).forEach(x -> System.out.println(x));``````

#### 数组拷贝

``````int[] arr = {3, 4, 9};
int[] arr2 = Arrays.copyOf(arr, arr.length);
System.out.println(Arrays.toString(arr2));``````

#### 数组填充与合并

###### 数组填充

``````int[] arr = new int[10];
Arrays.fill(arr, 6);
System.out.println(Arrays.toString(arr));``````

###### 数组合并

``````int[] arr = {2, 8, 13, 11, 6, 7};
int[] arr2 = {66, 88};
// 合并数组
System.out.println(Arrays.toString(arr3));``````

#### 排序与算法

###### 数组排序

``````int[] arr = {2, 8, 13, 11, 6, 7};
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));``````

###### 数组逆序

``````int[] arr = {2, 8, 13, 11, 6, 7};
int[] arr = {2, 8, 13, 11, 6, 7};
// 数组正序（排序）
Arrays.sort(arr);
// 数组逆序
org.apache.commons.lang3.ArrayUtils.reverse(arr);
System.out.println(Arrays.toString(arr));``````

###### 冒泡排序

``````int[] arr = {2, 8, 13, 11, 6, 7};
System.out.println("排序前：" + Arrays.toString(arr));
for (int i = 0; i < arr.length; i++) {
// 因为冒泡是把每轮循环中较大的数飘到后面，所以是 arr.length-i-1
for (int j = 0; j < arr.length - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// 元素交换
int temp = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = temp;
}
}
}
System.out.println("排序后：" + Arrays.toString(arr));``````

``````排序前：[2, 8, 13, 11, 6, 7]

###### 选择排序

``````int[] arr = {2, 8, 13, 11, 6, 7};
System.out.println("排序前：" + Arrays.toString(arr));
for (int i = 0; i < arr.length; i++) {
int lowerIndex = i;
for (int j = i + 1; j < arr.length; j++) {
// 找出最小的一个索引
if (arr[j] < arr[lowerIndex]) {
lowerIndex = j;
}
}
// 交换
int temp = arr[i];
arr[i] = arr[lowerIndex];
arr[lowerIndex] = temp;
}
System.out.println("排序后：" + Arrays.toString(arr));``````

``````排序前：[2, 8, 13, 11, 6, 7]

#### 元素查找

``````int[] arr = {1, 3, 4, 5};
// Arrays.binarySearch() 使用二分法查询某值
int index = Arrays.binarySearch(arr, 5);
System.out.println(index);``````

#### 多维数组

``````// 声明二维数组
int[][] arr = new int[2][4];
//循环二维数组
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[0].length; j++) {
// 二维数组赋值
arr[i][j] = j;
}
}
// 二维数组取值
System.out.println(arr[0][1]);
// 打印二维数组
System.out.println(Arrays.toString(arr[0]));
System.out.println(Arrays.toString(arr[1]));``````

``````1
[0, 1, 2, 3]
[0, 1, 2, 3]``````

#### 数组类型转换

###### 字符串转数组

``````String str = "laowang,stone,wanglei";
String[] arr = str.split(","); // 字符串转数组
System.out.println(arr[0]);``````
###### 数组转字符串

``````String[] arr = {"laowang", "stone", "wanglei"};
String str = Arrays.toString(arr);
System.out.println(str);``````

###### 数组转集合

``````String[] strArr = {"cat", "dog"};
List list = Arrays.asList(strArr);
System.out.println(list);``````
###### 集合转数组

``````List<String> list = new ArrayList<String>();
// 集合转换为数组
String[] arr = list.toArray(new String[list.size()]);
System.out.println(Arrays.toString(arr));``````

#### 相关面试题

##### 1. 数组和集合有什么区别？

• 集合可以存储任意类型的对象数据，数组只能存储同一种数据类型的数据；
• 集合的长度是会发生变化的，数组的长度是固定的；
• 集合相比数组功能更强大，数组相比集合效率更高。
##### 2. 以下代码访问数组元素打印的结果是多少？
``````int[] arr = new int[5] {1, 2, 3, 4, 5};
System.out.println(arr[4]);``````

``````int[] arr = new int[]{1, 2, 3, 4, 5};
System.out.println(arr[4]);``````

##### 3. 执行以下代码会输出什么结果？
``````public static void main(String[] args) {
int[] arr = {2, 3, 4, 8};
change(arr);
System.out.println(arr[2]);
}
private static void change(int[] arr) {
for (int i = 0; i < arr.length; i++) {
if (i % 2 == 0) {
arr[i] *= i;
}
}
}``````

##### 4. 以下程序打印的结果是多少？
``````int[] intArr = new int[3];
String[] StrArr = new String[3];
System.out.println(intArr[1]);
System.out.println(StrArr[1]);``````

##### 5. 数组转换字符串有哪些方式？

``````String[] arr = {"laowang", "stone", "wanglei"};
StringBuffer sb = new StringBuffer();
for (int i = 0; i < arr.length; i++) {
sb.append(arr[i]);
if (i != arr.length - 1)
sb.append(",");
}
System.out.println(sb.toString());``````

``````String[] arr = {"laowang", "stone", "wanglei"};
String str2 = Arrays.toString(arr);
System.out.println(str2);``````

``````String[] arr = {"laowang", "stone", "wanglei"};
String str3 = StringUtils.join(Arrays.asList(arr), ","); // 使用英文逗号分隔
System.out.println(str3);``````
##### 6. 数组遍历有哪几种方式？

• 传统 for 循环，如 for (int i = 0; i < arr.length; i++) { //...... }
• for each 循环，如 for (int i : arr) { //...... }
• jdk 8 Lambda 方式，如 `Integer[] arr = {2, 3, 6, 7, 9}; Arrays._asList_(arr).forEach(x -> System._out_.println(x));`
##### 7. 以下数组比较的结果分别是什么？
``````String[] strArr = {"dog", "cat", "pig", "bird"};
String[] strArr2 = {"dog", "cat", "pig", "bird"};
System.out.println(Arrays.equals(strArr, strArr2));
System.out.println(strArr.equals(strArr2));
System.out.println(strArr == strArr2);``````

``````public boolean equals(Object obj) {
return (this == obj);
}``````

``````public static boolean equals(Object[] a, Object[] a2) {
if (a==a2)
return true;
if (a==null || a2==null)
return false;
int length = a.length;
if (a2.length != length)
return false;
for (int i=0; i<length; i++) {
Object o1 = a[i];
Object o2 = a2[i];
if (!(o1==null ? o2==null : o1.equals(o2)))
return false;
}
return true;
}``````
##### 8. 以下程序使用 Arrays.binarySearch 返回的结果是 true 还是 false？
``````String[] arr = {"dog", "cat", "pig", "bird"};
int result = Arrays.binarySearch(arr, "bird");
System.out.println(result == -1);``````

``````String[] arr = {"dog", "cat", "pig", "bird"};
Arrays.sort(arr);
int result = Arrays.binarySearch(arr, "bird");
System.out.println(result == -1);``````
##### 9. Arrays 对象有哪些常用的方法？

• Arrays.copyOf() 数组拷贝
• Arrays.asList() 数组转为 List 集合
• Arrays.fill() 数组赋值
• Arrays.sort() 数组排序
• Arrays.toString() 数组转字符串
• Arrays.binarySearch() 二分法查询元素
• Arrays.equals() 比较两个数组的值
##### 10. 查询字符串数组中是否包含某个值有几种方法？

• 方式一：Arrays.asList(array).contains("key");
• 方式二：Arrays.binarySearch(array, "key");

``````String[] arr = {"doc", "pig", "cat"};
// 方式一：Arrays.asList(array).contains
boolean bool = Arrays.asList(arr).contains("cat");
System.out.println(bool);
// 方式二：Arrays.binarySearch
Arrays.sort(arr);
boolean bool2 = Arrays.binarySearch(arr, "cat") > -1;
System.out.println(bool2);``````
##### 11. 如何修改数组的第三个到第五个元素的值为 6？

``````int[] arrInt = new int[10];
for (int i = 0; i < arrInt.length; i++) {
if (i >= 2 && i < 5) {
arrInt[i] = 6;
}
}``````

``````int[] arrInt = new int[10];
Arrays.fill(arrInt, 2, 5, 6);``````