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

Java数组常用API的更多相关文章

  1. 数组常用API(1)

    数组常用API: 1. push 作用:数组尾部添加: 特点:可以添加一个或多个值: 返回值是数组最新的长度:会改变原数组: 示例: var arr = [10,20,30,40];          ...

  2. Java 之常用API(一)

    常用API  1 API概述  2 Scanner类与String类  3 StringBuilder类 NO.one API概述 1.1 API概述 API(Application Programm ...

  3. Java之常用API

    API概述 什么是API API (Application Programming Interface) :应用程序编程接口 java中的API 指的就是 JDK 中提供的各种功能的 Java类,这些 ...

  4. JavaScript之数组常用API

    这篇文章主要帮助大家简单理解数组的一些常用API用法,许多小伙伴常用方法记不住?别急,看完下面的介绍您一定就会明白各个方法是如何用的了.该文章适合新手小白看,大佬可以多多指点️! 1.数组的创建以及A ...

  5. Java 基础 常用API (Object类,String类,StringBuffer类)

    Java API Java 的API(API: Application(应用) Programming(程序) Interface(接口)) Java API就是JDK中提供给我们使用的类,这些类将底 ...

  6. Java 之常用API(二)

    Object类 & System类 日期相关类 包装类 & 正则表达式 Object类 & System类 1.1 Object类 1.1.1 概述 Object类是Java语 ...

  7. java自学-常用api

    API(Application Programming Interface),应用程序编程接口.Java API是JDK中提供给我们使用的类的说明文档.即jdk包里边写好的类,这些类将底层的代码实现封 ...

  8. Java的常用API

    Object类 1.toString方法在我们直接使用输出语句输出对象的时候,其实通过该对象调用了其toString()方法. 2.equals方法方法摘要:类默认继承了Object类,所以可以使用O ...

  9. Java的常用API之System类简介

    Syetem类 java.lang.System类中提供了大量的静态方法,可以获取与系统相关的信息或系统级操作,在System类的API文档中,常用的方法有: public static long c ...

随机推荐

  1. 【JavaScript】数组随机排序 之 Fisher–Yates 洗牌算法

    Fisher–Yates随机置乱算法也被称做高纳德置乱算法,通俗说就是生成一个有限集合的随机排列.Fisher-Yates随机置乱算法是无偏的,所以每个排列都是等可能的,当前使用的Fisher-Yat ...

  2. java多线程----ReentrantReadWriteLock

    package com.test; import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks ...

  3. nginx服务器的rewrite功能

    nginx编译的时候默认是把rewrite功能编辑进去的,但是因为rewrite需要使用正则表达式,因此需要安装pcre依赖包. yum install -y pcre pcre-install re ...

  4. SQL学习之SQL注入学习总结

    所谓SQL注入,就是通过把SQL命令插入到Web表单提交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令. 测试数据库 我们本文就以如下数据库作为测试数据库,完成我们的注入分析 ...

  5. Python Web学习笔记之Python多线程和多进程、协程入门

    进程和线程究竟是什么?如何使用进程和线程?什么场景下需要使用进程和线程?协程又是什么?协程和线程的关系和区别有哪些? 程序切换-CPU时间的分配 首先,我们的任何一个程序都需要运行在一个操作系统中,如 ...

  6. Linux查看文件大小命令

    Linux查看文件大小命令 du命令 (1)du -b filepath 参数-b表示以字节计数 du -b filepath 参数-b表示以字节计数 #示例: $ du -b ~/Downloads ...

  7. xml声明中的standalone属性

    晚上,在测试tinyxml的时候,发现其中声明了<?xml version="1.0" standalone="no" ?>,经查,其含义为stan ...

  8. powershell配置Gvim

    1.下载安装Gvim 我的安装目录在:C:\gVimPortable 配色方案目录:C:\gVimPortable\App\vim\vim72\colors 配置文件目录:C:\gVimPortabl ...

  9. 20145220韩旭飞《网络对抗》实验九:web安全基础实践

    基础问答 (1)SQL注入攻击原理,如何防御 原理:把SQL命令插入到Web表单递交或输入域名或页面请求的查询字符串. 防御: 利用输入规则限制进行防御,不允许特殊字符输入 (2)XSS攻击的原理,如 ...

  10. 20145220韩旭飞《网络对抗》Exp2 后门原理与实践

    20145220韩旭飞<网络对抗>Exp2 后门原理与实践 常用后门工具实践 Windows获得Linux Shell 在Windows下,先使用ipconfig指令查看本机IP: 使用n ...