Java数据结构---Array(数组)
1. Array(矩阵)
1.1 一维数组
- 声明数组
dataType[] arrayRefVar; // 如 int[] res ,声明一个名为res的数组
- 创建数组
arrayRefVar = new dataType[arraySize]; // 如 res = new int[5] ,创建一个长度为5的数组
arrayRefVar = int[]{1, 2, 3 ,4 ,5 ,6}; // 创建一个数组:[1,2,3,4,5,6]
- 以上可整合成一条代码
int[] res = new int[]{1, 2, 3 ,4 ,5 ,6};
int[] res_1 = new int[];
String[] res_2 = new String[];
1.2 二维数组(矩阵)
- 创建矩阵
int[][] res = new int[3][4]; // 创建一个名为res的3*4矩阵
2. 其他操作
- 给数组赋值
res.fill(0);
- 对数组排序(按升序)
res.sort();
- 比较数组中元素值是否相等
res.equals(res_b);
- 对排序好的数组进行二分查找法操作
res.binarySearch(5);
- 获取二维数组的行和列
int row = res.length; // 行数
int col = res[0].length; // 列数
6. 给数组排序(数组中最有用的操作)
int[] res = new int[]{2, 3, 5 ,1 ,4 ,6};
Arrays.sort(res); // 排序为后输出为 [1,2,3,4,5,6]