Java数组(4):数组实用功能
Java标准类库的System.arraycopy()方法,及在java.utils.Arrays类中一套用于数组的static方法,都是操纵数组实用功能。下面分别介绍。
(1) 数组的复制
(2) 数组的比较
(3) 数组的排序和查找
(1) 数组的复制
System.arraycopy(源数组, 从源数组的什么位置开始复制的偏移量, 目标数组, 从标数数组的什么位置开始复制的偏移量, 需要复制的元素的个数)
System.arraycopy已经对所有基本类型(包装类型同样适用)做了重载,如果复制对象,则只是浅复制(复制引用)。
下面以int作为示例:
import java.util.Arrays; public class Test1 {
public static void main(String[] args) {
int[] i = new int[3];
int[] j = new int[7];
Arrays.fill(i, 47);
Arrays.fill(j, 99);
System.out.println("i = " + Arrays.toString(i)); // i = [47, 47, 47]
System.out.println("j = " + Arrays.toString(j)); // j = [99, 99, 99, 99, 99, 99, 99]
int[] k = new int[5];
Arrays.fill(k, 103);
System.arraycopy(i, 0, k, 0, i.length);
System.out.println("k = " + Arrays.toString(k)); // k = [47, 47, 47, 103, 103]
System.arraycopy(k, 1, j, 0, 4); //
System.out.println("j = " + Arrays.toString(j)); // j = [47, 47, 103, 103, 99, 99, 99]
}
}
(2) 数组的比较
同样,Arrays.equals已经对所有基本类型(包装类型同样适用)做了重载,对于对象比较,使用了对象的equals方法,所以必须重写对象的equals方法。
import java.util.Arrays; public class Test2 {
public static void main(String[] args) {
int[] a1 = new int[5];
int[] a2 = new int[5];
Arrays.fill(a1, 47);
Arrays.fill(a2, 47);
System.out.println(Arrays.equals(a1, a2)); // true
a2[3] = 11;
System.out.println(Arrays.equals(a1, a2)); // false
String[] s1 = new String[4];
Arrays.fill(s1, "Hi");
String[] s2 = { "Hi", "Hi", new String("Hi"), new String("Hi") };
System.out.println(Arrays.equals(s1, s2)); // true
}
}
(3) 数组的排序和查找
同样,Arrays.sort已经对所有基本类型(包装类型同样适用)做了重载,对于对象比较,必须实现Comparable接口。
如果数组已经排好序,就可以使用Arrays.binarySearch执行快速查找(前提必须是排好序的数组)。
如果使用了Comparator<T>排序了某个对象数组,使用Arrays.binarySearch时必须提供同样的Comparator<T>。
import java.util.Arrays;
import java.util.Collections;
import java.util.Random; class CompType implements Comparable<CompType> {
int i;
int j; public CompType(int n1, int n2) {
i = n1;
j = n2;
} @Override
public String toString() {
return "(" + i + ", " + j + ")";
} @Override
public int compareTo(CompType ct) {
return i == ct.i ? Integer.compare(j, ct.j) : Integer.compare(i, ct.i);
}
} public class Test3 {
public static void main(String[] args) { // 基本类型
Random random = new Random(47);
int[] a = random.ints(5, 5, 10).toArray();
System.out.println(Arrays.toString(a)); // [8, 5, 8, 6, 6]
Arrays.sort(a); // Arrays.sort(基本类型)
System.out.println(Arrays.toString(a)); // [5, 6, 6, 8, 8] // 包装类型
Integer[] b = { 3, 5, 9, 8, 2 };
Arrays.sort(b); // Arrays.sort(Object)
System.out.println(Arrays.toString(b)); // [2, 3, 5, 8, 9]
Arrays.sort(b, Collections.reverseOrder()); // Arrays.sort(Object, Comparator<T>)
System.out.println(Arrays.toString(b)); // [9, 8, 5, 3, 2] // String
String[] c = { "A", "B", "AB", "AC", "a", "b", "ab", "ac" };
Arrays.sort(c); // 字符串默认是字典排序[A-Za-z]
System.out.println(Arrays.toString(c)); // [A, AB, AC, B, a, ab, ac, b]
Arrays.sort(c, String.CASE_INSENSITIVE_ORDER); // 忽略大小写排序
System.out.println(Arrays.toString(c)); // [A, a, AB, ab, AC, ac, B, b] // 对象类型
CompType[] d = { new CompType(2, 2), new CompType(1, 2), new CompType(2, 4), new CompType(0, 3),
new CompType(3, 4), new CompType(3, 0), new CompType(2, 2), new CompType(2, 1) };
Arrays.sort(d);
System.out.println(Arrays.toString(d)); // [(0, 3), (1, 2), (2, 1), (2, 2), (2, 2), (2, 4), (3, 0), (3, 4)]
Arrays.sort(d, Collections.reverseOrder());
System.out.println(Arrays.toString(d)); // [(3, 4), (3, 0), (2, 4), (2, 2), (2, 2), (2, 1), (1, 2), (0, 3)] // 快速查找
Arrays.sort(b);
int location = Arrays.binarySearch(b, 8);
System.out.println("Location of [5] is " + location + ", b[" + location + "] = " + b[location]); // Location of [5] is 3, b[3] = 8 Arrays.sort(c);
location = Arrays.binarySearch(c, "AC");
System.out.println("Location of [AC] is " + location + ", c[" + location + "] = " + c[location]); // Location of [AC] is 2, c[2] = AC Arrays.sort(c, String.CASE_INSENSITIVE_ORDER);
location = Arrays.binarySearch(c, "AC", String.CASE_INSENSITIVE_ORDER);
System.out.println("Location of [AC] is " + location + ", c[" + location + "] = " + c[location]); // Location of [AC] is 5, c[5] = ac Arrays.sort(d);
location = Arrays.binarySearch(d, new CompType(2, 4));
System.out.println("Location of (2, 4) is " + location + ", d[" + location + "] = " + d[location]); // Location of (2, 4) is 5, d[5] = (2, 4)
}
}
Java数组(4):数组实用功能的更多相关文章
- Java反射遍历数组
日志中有时候需要查看数组中的值,但是重载很多的打印函数,觉得很别扭.所以想通过反射,获取数组中的值,打印出来.Java提供了数组反射操作的类,之前没有关注过,提供的方法简单易用. public sta ...
- 《数据结构》 java的一维数组的内存结构与其特性
1{数组的概念: 数组是相同类型变量的集合,可以使用共同的名字引用它.数组也可以被定义为任何类型,可以是一维或者二维的.数组的访问时通过其对应的下标来实现的.数组提供了一种将有联系的信息便利分组的方式 ...
- Java中二维数组与面向对象
1:二维数组(理解) (1)元素是一维数组的数组. (2)格式: A:数据类型[][] 数组名 = new 数据类型[m][n]; B:数据类型[][] 数组名 = new 数据类型[m][]; C: ...
- 慕课网-安卓工程师初养成-6-3 如何使用 Java 中的数组
来源:http://www.imooc.com/code/1525 Java 中操作数组只需要四个步骤: 1. 声明数组 语法: 或者 其中,数组名可以是任意合法的变量名,如: 2. 分配空间 简 ...
- Android笔记:java 中的数组
在与嵌入式设备通讯的过程中使用的socket通讯 获取的字节流,通常转换为字节数组,需要根据协议将字节数组拆分.对于有规律的重复拆分可以使用,由于java中不能像c中直接进行内存操作例如使用struc ...
- Java中的数组操作进阶
package com.mi.array; import java.util.Arrays; /** * System.arraycopy(i, 0, j, 0, i.length);这种复制会覆盖目 ...
- 黑马程序员——JAVA基础之数组
------- android培训.java培训.期待与您交流! ---------- 数组: 数组的定义: 数组是相同类型数据的集合, 描述的是相同类型的若干个数据按照一定的先后顺序排列组合而成,其 ...
- 如何使用 Java 中的数组
Java 中操作数组只需要四个步骤: 1. 声明数组 语法: 数据类型[ ] 数组名: 或者 数据类型 数组名[ ]: 其中,数组名可以是任意合法的变量名,如: 2. 分配空间 简单地说,就是指 ...
- Java之组合数组1
我们先说"数组",数组是有序数据的集合,数组中的每个元素具有相同的数组名和下标来唯一地确定数组中的元素. 一.一维数组的定义 type arrayName[]; 其中类型(type ...
- Java比较器对数组,集合排序一
数组排序非常简单,有前辈们的各种排序算法,再加上Java中强大的数组辅助类Arrays与集合辅助类Collections,使得排序变得非常简单,如果说结合比较器Comparator接口和Collato ...
随机推荐
- FlexPaper的深入了解和应用
作者:tabb_ 零下疯度 推荐:无痕客 最近做项目需要用到flexpaper,所以想借此机会好好的研究一下. 这是官方的下载地址:http://flexpaper.devaldi.com/downl ...
- CodeForces 839C - Journey | Codeforces Round #428 (Div. 2)
起初误以为到每个叶子的概率一样于是.... /* CodeForces 839C - Journey [ DFS,期望 ] | Codeforces Round #428 (Div. 2) */ #i ...
- HDU 6049 - Sdjpx Is Happy | 2017 Multi-University Training Contest 2
思路来源于 FXXL - - 一个比较奇怪的地方就是第三步可以不做,也就是ans至少为1,听说场内有提问的,然后 admin 说可以不做- - (wa的我心烦) /* HDU 6049 - Sdjpx ...
- learning armbian steps(7) ----- armbian 源码分析(二)
从compile.sh开始入手: SRC="$(dirname "$(realpath "${BASH_SOURCE}")")" # fal ...
- 「CF724G」Xor-matic Number of the Graph「线性基」
题意 求所有点对\(u,v\),\(u\)到\(v\)所有不同的异或路径的异或值之和,对\(10^9+7\)取模 题解 求出一个dfs树,那么\(u\)到\(v\)的路径一定是树上路径异或一些环.这些 ...
- python2.X与Python3.X区别
__future__模块 [回到目录] Python 3.x引入了一些与Python 2不兼容的关键字和特性,在Python 2中,可以通过内置的__future__模块导入这些新内容.如果你希望在P ...
- Codeforces 915 F. Imbalance Value of a Tree(并查集)
F. Imbalance Value of a Tree 题意: 给一颗带点权的树,求所有简单路径上最大点权和最小点权之差的总和. 思路: 所求问题可以看作求各路径上的最大值之和减各路径上的最小值之和 ...
- DOM元素
元素的三种创建方法 1.doumrnt.write(可以写类名,各种表情属性) script写在哪里就创建在哪 <button>点我</button> <sc ...
- python并发——从线程池获取返回值
并发是快速处理大量相似任务的绝佳办法,但对于有返回值的方法,需要一个容器专门来存储每个进程处理完的结果 from multiprocessing import Pool import time #返回 ...
- Leetcode题目62.不同路径(动态规划-中等)
题目描述: 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” ). 机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角(在下图中标记为“Finish”). ...