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)
}
}

最新文章

  1. python 三元运算
  2. 微软的R语言发行版本MRO及开发工具RTVS
  3. 2014年6月份第3周51Aspx源码发布详情
  4. 数据存储与IO(一)
  5. 使用Eclipse将Web项目打Jar包方法
  6. Redundant Paths-POJ3177(并查集+双连通分量)
  7. 设置Eclipse支持C++ 11
  8. C++检测一个文件是否存在
  9. Matlab删除NaN数据
  10. (转)Spring整合Redis作为缓存
  11. 安卓学习之ListView和GridView
  12. hdu4722 Good Numbers
  13. 从Java到C (大纲)
  14. 【高性能】生成唯一时间戳ID,1毫秒预计能生成1000个
  15. Neo4j学习笔记(1)——使用API编写一个Hello World程序
  16. SQLOS任务调度算法
  17. Windows10下Django虚拟环境配置和简单入门实例
  18. 编译wxWidgets
  19. [HNOI2010]CITY 城市建设
  20. 【Jmeter基础知识】Jmeter响应断言和断言结果

热门文章

  1. okhttp初识拦截器
  2. Spring入门篇——第6章 Spring AOP的API介绍
  3. CSP-S2019 退役记/赛后总结
  4. 前端知识体系:JavaScript基础-变量和类型
  5. remmina连接xfce桌面的centos7
  6. ORM高阶补充:only, defer,select_related
  7. html([val|fn])
  8. 用JavaScript实现快排
  9. 视频断点续传+java视频
  10. javascript JSON.parse and JSON.stringify