java.util.Arrays
Arrays.asList()
数组转换成列表
String[] strArray = {"zhang", "xue", "zhi"};
List<String> list = Arrays.asList(strArray);
// 打印元素
for (int i=0; i<list.size(); i++) {
System.out.print(list.get(i) + " ");
}
1
2
3
4
5
6
Arrays.binarySearch()
二分查找
查找前,一定要排序。
如果查找元素不存在,返回(-(insertion point) - 1)。
自然数表示查到,负数表示没有查找。
int[] a = {3,5,9,7,2};
Arrays.sort(a); // 排序
// 打印数组
for (int item : a)
System.out.print(item + " ");
System.out.println();
// 二分查找
int ind1 = Arrays.binarySearch(a, 2);
int ind2 = Arrays.binarySearch(a, 4);
int ind3 = Arrays.binarySearch(a, 1, 3, 5);
System.out.println("2的查找位置:" + ind1);
System.out.println("4的查找位置:" + ind2);
System.out.println("5的查找位置:" + ind3); // 字符串
String[] strArray = {"aa", "bc", "ab", "cd"};
Arrays.sort(strArray);
int ind4 = Arrays.binarySearch(strArray, "bc");
System.out.println("'bc'的查找位置是:" + ind4);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Arrays.copyOf()
复制长度大于原数组长度时,后面补零。
int[] a = {3,5,9,7,2};
int[] newa = Arrays.copyOf(a, 2);
int[] newa2 = Arrays.copyOf(a, 7); //复制长度大于原数组的长度 for (int item : newa)
System.out.print(item + " ");
System.out.println(); for (int item : newa2)
System.out.print(item + " ");
System.out.println();
1
2
3
4
5
6
7
8
9
10
11
Arrays.copyOfRange()
复制长度大于原数组长度时,后面补零。
Java中区间一般都是左闭右开[a,b),即包括左边,不包括右边。
int[] a = {3,5,9,7,2};
int[] newa = Arrays.copyOfRange(a, 1, 3);
int[] newa2 = Arrays.copyOfRange(a, 1, 8); //复制长度大于原数组的长度 for (int item : newa)
System.out.print(item + " ");
System.out.println(); for (int item : newa2)
System.out.print(item + " ");
System.out.println();
1
2
3
4
5
6
7
8
9
10
11
Arrays.deepEquals()
比较数组元素是否深层相等。
一维数组无区别,高维数组有区别。
String[][] ticTacToe1 = { { " O ", " O ", " X " }, { " O ", " X ", " X " },{ " X ", " O ", " O " } };
String[][] ticTacToe2 = { { " O ", " O ", " X " }, { " O ", " X ", " X " },{ " X ", " O ", " O " } };
System.out.println(Arrays.equals(ticTacToe1, ticTacToe2));// false
System.out.println(Arrays.deepEquals(ticTacToe1, ticTacToe2));// true String[] ticTacToe3 = { " O ", " O ", " X " };
String[] ticTacToe4 = { " O ", " O ", " X " };
System.out.println(Arrays.equals(ticTacToe3, ticTacToe4)); // true
System.out.println(Arrays.deepEquals(ticTacToe3, ticTacToe4)); // true
1
2
3
4
5
6
7
8
9
Arrays.deepHashCode()
深层相等的两个数组的深层哈希编码也相等。
String[][] ticTacToe1 = { { " O ", " O ", " X " }, { " O ", " X ", " X " },{ " X ", " O ", " O " } };
String[][] ticTacToe2 = { { " O ", " O ", " X " }, { " O ", " X ", " X " },{ " X ", " O ", " O " } };
System.out.println(Arrays.equals(ticTacToe1, ticTacToe2));// false
System.out.println(Arrays.deepEquals(ticTacToe1, ticTacToe2));// true
// ticTacToe1和ticTacToe1深层相等,深层哈希编码也相等。
System.out.println(Arrays.deepHashCode(ticTacToe1));
System.out.println(Arrays.deepHashCode(ticTacToe2));
1
2
3
4
5
6
7
Arrays.deepToString()
// 二维数组有区别
String[][] ticTacToe1 = { { " O ", " O ", " X " }, { " O ", " X ", " X " },{ " X ", " O ", " O " } };
System.out.println(Arrays.deepToString(ticTacToe1)); // 深层变换成字符串
System.out.println(Arrays.toString(ticTacToe1)); // 一般 // 一维数组无区别
String[] ticTacToe3 = { " O ", " O ", " X " };
System.out.println(Arrays.deepToString(ticTacToe3)); // 深层变换成字符串
System.out.println(Arrays.toString(ticTacToe3)); // 一般
1
2
3
4
5
6
7
8
9
Arrays.equals()
参考Arrays.deepArrays()
Arrays.fill()
填充数组元素
int[] a = {1,2,3,4,5,6}; Arrays.fill(a, 8);//全部填充
for (int item : a)
System.out.print(item + " ");
System.out.println(); Arrays.fill(a, 1, 3, 0);//指定范围,替换
for (int item : a)
System.out.print(item + " ");
1
2
3
4
5
6
7
8
9
10
Arrays.hashCode()
如果两个数组相等,哈希编码也相等。
int[] a = {1,2,3,4,5,6};
int[] b = {1,2,3,4,5,6};
String[] c = {"a", "b", "c"};
String[] d = {"a", "b", "c"}; System.out.println("a==b:" + Arrays.equals(a, b));
System.out.println("a和b的哈希码分别为:" + Arrays.hashCode(a) + "\t" + Arrays.hashCode(b));
// 字符串
System.out.println("c==d:" + Arrays.equals(c, d));
System.out.println("c和d的哈希码分别为:" + Arrays.hashCode(c) + "\t" + Arrays.hashCode(d)); System.out.println("c==d:" + c.equals(d));
System.out.println("c和d的哈希码分别为:" + c.hashCode() + "\t" + d.hashCode());
1
2
3
4
5
6
7
8
9
10
11
12
13
Arrays.sort()
int[] a = {6,5,4,3,2,1};
// 指定范围排序
Arrays.sort(a, 1, 4);
for (int item : a)
System.out.print(item + " ");
System.out.println();
// 全部元素排序
Arrays.sort(a);
for (int item : a)
System.out.print(item + " ");
System.out.println();
1
2
3
4
5
6
7
8
9
10
11
Arrays.toString()
参考 Arrays.deepToString()
遍历数组
遍历一维数组
int[] a = {6,5,4,3,2,1};
// 遍历数组 for
for (int i=0; i<a.length; i++)
if (i == a.length-1)
System.out.print(a[i]);
else
System.out.print(a[i] + ", ");
System.out.println(); // 遍历数组 - foreach
for (int item : a)
if (item == a[a.length-1])
System.out.print(item);
else
System.out.print(item + ", ");
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
遍历二维数组
int[][] a = {{1,2,3}, {4,5,6}, {7,8,9}};
// 遍历二维数组 for
for (int i=0; i<a.length; i++) {
for (int j=0; j<a[i].length; j++)
if (j == a[i].length-1)
System.out.print(a[i][j]);
else
System.out.print(a[i][j] + ", ");
System.out.println();
}
1
2
3
4
5
6
7
8
9
10
数组对象的方法
数组从java.lang.Object继承的方法:clone, equals, finalize, getClass, hashCode, notify, toString, wait
arr.clone()
通过克隆生成另一个数组
int[] a = {1,2,3,4,5,6};
int[] b = a.clone();
Arrays.fill(a, 1, 4, 0); // 改变a,看是否对b有影响
//打印b
for (int item : b)
System.out.print(item + " ");
1
2
3
4
5
6
arr.equals()
arr.equals()和Arrays.equals()不同
int[] a = {1,2,3,4,5,6};
int[] b = a.clone();
int[] c = a;
// 判等
System.out.println(Arrays.equals(a, b)); // 比较内容
System.out.println(a.equals(b)); //比较地址
System.out.println(a.equals(c));
1
2
3
4
5
6
7
arr.getClass()
int[] a = {1,2,3,4,5,6}; System.out.println(a.getClass());
1
2
3
arr.hashCode()
int[] a = {1,2,3,4,5,6}; // 两种方法的结果不同。
System.out.println(a.hashCode());
System.out.println(Arrays.hashCode(a)); // Arrays.hashCode()
1
2
3
4
5
arr.toString()
int[] a = {1,2,3,4,5,6}; System.out.println(a.toString()); // 地址
System.out.println(Arrays.toString(a)); //字符串
1
2
3
4
数组对象的属性
arr.length
int[] a = {1,2,3,4,5,6}; int len = a.length;
System.out.println(len);

转自:https://blog.csdn.net/xuezhisdc/article/details/52346800

参考:https://blog.csdn.net/qq_19558705/article/details/50436583

最新文章

  1. 请求如何进入ASP.NET MVC框架
  2. DOM中的事件对象
  3. iOS的几种定时器
  4. 考研路之C语言
  5. windows2008 x86 安装 32位oracle
  6. SGU 181.X-Sequence
  7. OpenGL ES 2.0 混合
  8. linux crontab设置
  9. Mysql之CentOS初探
  10. C# 语言规范_版本5.0 (第1章 介绍)
  11. var d = document.getElementById 错误
  12. 【Java数据结构学习笔记之二】Java数据结构与算法之队列(Queue)实现
  13. linux压缩相关命令
  14. [LeetCode] Employee Importance 员工重要度
  15. Ocelot中文文档-管理
  16. Javascript高级编程学习笔记(47)—— 元素遍历
  17. MySQL的一些基本命令笔记(2)
  18. 关于 ubuntu 下 防火墙 ufw的使用
  19. java_11接口
  20. vue-cli中全局组件的注册使用

热门文章

  1. tomcat启动失败问题排除及解决办法 Server Tomcat v7.0 Server at localhost failed to start.
  2. Mysql优化_内置profiling性能分析工具
  3. UVA644 Immediate Decodability
  4. trace
  5. ubuntu16.04下内核模块解析
  6. 明码|2018年蓝桥杯B组题解析第二题-fishers
  7. keepalived主从及双主配置
  8. v-model双向数据绑定
  9. 在线js编程网站 精品版
  10. UVa 225 黄金图形(回溯+剪枝)