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):数组实用功能的更多相关文章

  1. Java反射遍历数组

    日志中有时候需要查看数组中的值,但是重载很多的打印函数,觉得很别扭.所以想通过反射,获取数组中的值,打印出来.Java提供了数组反射操作的类,之前没有关注过,提供的方法简单易用. public sta ...

  2. 《数据结构》 java的一维数组的内存结构与其特性

    1{数组的概念: 数组是相同类型变量的集合,可以使用共同的名字引用它.数组也可以被定义为任何类型,可以是一维或者二维的.数组的访问时通过其对应的下标来实现的.数组提供了一种将有联系的信息便利分组的方式 ...

  3. Java中二维数组与面向对象

    1:二维数组(理解) (1)元素是一维数组的数组. (2)格式: A:数据类型[][] 数组名 = new 数据类型[m][n]; B:数据类型[][] 数组名 = new 数据类型[m][]; C: ...

  4. 慕课网-安卓工程师初养成-6-3 如何使用 Java 中的数组

    来源:http://www.imooc.com/code/1525 Java 中操作数组只需要四个步骤: 1. 声明数组 语法:  或者  其中,数组名可以是任意合法的变量名,如: 2. 分配空间 简 ...

  5. Android笔记:java 中的数组

    在与嵌入式设备通讯的过程中使用的socket通讯 获取的字节流,通常转换为字节数组,需要根据协议将字节数组拆分.对于有规律的重复拆分可以使用,由于java中不能像c中直接进行内存操作例如使用struc ...

  6. Java中的数组操作进阶

    package com.mi.array; import java.util.Arrays; /** * System.arraycopy(i, 0, j, 0, i.length);这种复制会覆盖目 ...

  7. 黑马程序员——JAVA基础之数组

    ------- android培训.java培训.期待与您交流! ---------- 数组: 数组的定义: 数组是相同类型数据的集合, 描述的是相同类型的若干个数据按照一定的先后顺序排列组合而成,其 ...

  8. 如何使用 Java 中的数组

    Java 中操作数组只需要四个步骤: 1. 声明数组 语法:  数据类型[ ] 数组名: 或者   数据类型 数组名[ ]: 其中,数组名可以是任意合法的变量名,如: 2. 分配空间 简单地说,就是指 ...

  9. Java之组合数组1

    我们先说"数组",数组是有序数据的集合,数组中的每个元素具有相同的数组名和下标来唯一地确定数组中的元素. 一.一维数组的定义 type arrayName[]; 其中类型(type ...

  10. Java比较器对数组,集合排序一

    数组排序非常简单,有前辈们的各种排序算法,再加上Java中强大的数组辅助类Arrays与集合辅助类Collections,使得排序变得非常简单,如果说结合比较器Comparator接口和Collato ...

随机推荐

  1. 配置linux ftp服务器,客户端报list remote folder fail

    XFTP出现列表文件夹失败.主要是因为FTP模式不对,应该为主动连接模式, 可以在设置主机属性 - 选项页签 - 将默认勾选的“使用被动模式”(使用消极模式)的多选框取消...就可以了...

  2. Spring入门篇——第5章 Spring AOP基本概念

    第5章 Spring AOP基本概念 本章介绍Spring中AOP的基本概念和应用. 5-1 AOP基本概念及特点 5-2 配置切面aspect ref:引用另外一个Bean 5-3 配置切入点Poi ...

  3. linux安装libreOffice

    参考链接:https://qtdebug.com/mac-centos7-libreoffice/ https://blog.csdn.net/diyiday/article/details/7985 ...

  4. [Docker] Run a command inside Docker container

    For example you are working in a backend project, you have setup Dockerfile: FROM node:10.16.0-stret ...

  5. [jenkins] 启动错误 Failed to start LSB: Jenkins Automation Server.

    解决办法见https://blog.csdn.net/qq_34208844/article/details/87865672

  6. 010_linuxC++之_运算符重载

    (一)运算符重载:运算符重载,就是对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型. (二)实现类不同对象里中变量的相加 (三)程序 #include <iostream> ...

  7. 【luoguP4124 】[CQOI2016]手机号码

    题目描述 人们选择手机号码时都希望号码好记.吉利.比如号码中含有几位相邻的相同数字.不含谐音不吉利的数字等.手机运营商在发行新号码时也会考虑这些因素,从号段中选取含有某些特征的号码单独出售.为了便于前 ...

  8. MIME协议(五) -- MIME邮件的编码方式

    5  MIME邮件的编码方式 由于每个ASCII码字符只占用一个字节(8个bit位),且最高bit位总为0,即ASCII码字符中的有真正意义的信息只是后面的7个低bit位,而传统的SMTP协议又是基于 ...

  9. Codeforces 940 E.Cashback (单调队列,dp)

    Codeforces 940 E.Cashback 题意:一组数,要分为若干个区间,每个区间长度为ki(1<=ki<=n),并且对于每个区间删去前ki/c(向下取整)个小的数(即对区间升序 ...

  10. Tomcat7修改根路径应用

    大家想必遇到过这样的问题,同台机器上跑上多个tomcat 那么随之更改文件的工作量就会增加 今天突然想到把所有的tomcat的根目录更改成一个文件 但是有不好的就的地方就是在更改文件的时候需要把这台机 ...