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 ...
随机推荐
- thymeleaf小知识
1.根据不同性别,显明不同的默认图片:th:if th:src 图片路径 <img th:if="${gender=='男'}" id="admission_p ...
- sql查询数据结果发送到邮箱
#!/bin/bash user=root password=xx dbname=xx DATE=`date +%F` #注意:此处mysql要用全路劲,否则计划任务会执行失败 /mydata/mys ...
- Jmeter设置集合点(并发测试)
什么是集合点? 让所有请求在不满足条件的时候处于等待状态. 如何实现? 使用jmeter中的同步计时器Synchronizing Timer来实现 集合点的位置 因为集合点是在取样器sampler(例 ...
- tar命令--数据解档(三)解压.tar.gz文件报错 gzip:stdin:not in gzip format
毕竟是生产..... 提示以下信息: gzip: stdin: not in gzip format tar: Child returned status 1 tar: Error is not ...
- vue-cli3构建多页面应用2.0
1.0版本点这里 -> 博客:vue-cli3构建多页面应用1.0 github:vue-cli-multipage 在1.0版本上做了以下改进: 1. 增加pages.config.js, ...
- jQuery系列(十一):jQuery的事件绑定和解绑
1.绑定事件 语法: bind(type,data,fn) 描述:为每一个匹配元素的特定事件(像click)绑定一个事件处理器函数. 参数解释: type (String) : 事件类型 data ( ...
- 数据结构实验之链表二:逆序建立链表(SDUT 2117)
题目链接 #include <bits/stdc++.h> using namespace std; struct node { int data; struct node *next; ...
- codeforces163D
Large Refrigerator CodeForces - 163D 给定一个长方体的体积V,求出这个长方体的最小表面积. 输入 第一行有一个整数t (1 ≤ t ≤ 500) — 测试数据的组数 ...
- main.js中import引入css与引入js的区别
表现:引入css样式文件能够作用到全局,而引入js文件就只能在main.js中产生作用 在 main.js 中引入的 css 都是全局生效的.引入的 js 文件只在 main.js 中生效,是因为 m ...
- mysql 从一个表中查数据并插入另一个表实现方法
类别一. 如果两张张表(导出表和目标表)的字段一致,并且希望插入全部数据,可以用这种方法: INSERT INTO 目标表 SELECT * FROM 来源表 ; 例如,要将 articles ...