java.util.Arrays
Arrays.asList()
- 数组转换成列表
1 | String[] strArray = {"zhang", "xue", "zhi"}; |
Arrays.binarySearch()
- 二分查找
- 查找前,一定要排序。
- 如果查找元素不存在,返回(-(insertion point) - 1)。
- 自然数表示查到,负数表示没有查找。
1 | int[] a = {3,5,9,7,2}; |
Arrays.copyOf()
- 复制长度大于原数组长度时,后面补零。
1 | int[] a = {3,5,9,7,2}; |
Arrays.copyOfRange()
- 复制长度大于原数组长度时,后面补零。
- Java中区间一般都是左闭右开[a,b),即包括左边,不包括右边。
1 | int[] a = {3,5,9,7,2}; |
Arrays.deepEquals()
- 比较数组元素是否深层相等。
- 一维数组无区别,高维数组有区别。
1 | String[][] ticTacToe1 = { { " O ", " O ", " X " }, { " O ", " X ", " X " },{ " X ", " O ", " O " } }; |
Arrays.deepHashCode()
- 深层相等的两个数组的深层哈希编码也相等。
1 | String[][] ticTacToe1 = { { " O ", " O ", " X " }, { " O ", " X ", " X " },{ " X ", " O ", " O " } }; |
Arrays.deepToString()
1 | // 二维数组有区别 |
Arrays.equals()
- 参考Arrays.deepArrays()
Arrays.fill()
- 填充数组元素
1 | int[] a = {1,2,3,4,5,6}; |
Arrays.hashCode()
- 如果两个数组相等,哈希编码也相等。
1 | int[] a = {1,2,3,4,5,6}; |
Arrays.sort()
1 | int[] a = {6,5,4,3,2,1}; |
Arrays.toString()
- 参考 Arrays.deepToString()
遍历数组
遍历一维数组
1 | int[] a = {6,5,4,3,2,1}; |
遍历二维数组
1 | int[][] a = {{1,2,3}, {4,5,6}, {7,8,9}}; |
数组对象的方法
- 数组从java.lang.Object继承的方法:clone, equals, finalize, getClass, hashCode, notify, toString, wait
arr.clone()
- 通过克隆生成另一个数组
1 | int[] a = {1,2,3,4,5,6}; |
arr.equals()
- arr.equals()和Arrays.equals()不同
1 | int[] a = {1,2,3,4,5,6}; |
arr.getClass()
1 | int[] a = {1,2,3,4,5,6}; |
arr.hashCode()
1 | int[] a = {1,2,3,4,5,6}; |
arr.toString()
1 | int[] a = {1,2,3,4,5,6}; |
数组对象的属性
arr.length
1 | int[] a = {1,2,3,4,5,6}; |