JAVA核心技术I---JAVA基础知识(工具类Arrays和Collections类)

一:工具类

–不存储数据,而是在数据容器上,实现高效操作
• 排序
• 搜索
–Arrays类
–Collection类

二:Arrays类(处理数组)

(一)基本方法

–排序:对数组排序, sort/parallelSort。
–查找:从数组中查找一个元素, binarySearch。
–批量拷贝:从源数组批量复制元素到目标数组, copyOf
–批量赋值:对数组进行批量赋值, fill。
–等价性比较:判定两个数组内容是否相同, equals。

(二)实现

import java.util.Arrays;
import java.util.Random; public class ArraysTest {
public static void main(String[] args) {
//testSort();
//SearchSort();
//testCopy();
//testFill();
testEquality();
} public static void testSort() {
Random rm=new Random();
int[] a=new int[]; System.out.println("=======测试排序======"); for(int i=;i<a.length;i++) {
a[i] = rm.nextInt();
} System.out.println("===排序前===");
for(int i=;i<a.length;i++) {
System.out.print(a[i] + ",");
}
System.out.println(); System.out.println("===排序后===");
Arrays.sort(a); for(int i=;i<a.length;i++) {
System.out.print(a[i] + ",");
}
System.out.println();
} public static void SearchSort() {
Random rm=new Random();
int[] a=new int[]; System.out.println("=======测试查找======"); for(int i=;i<a.length;i++) {
a[i] = rm.nextInt();
} a[] = ;
Arrays.sort(a); System.out.println("===查找前===");
for(int i=;i<a.length;i++) {
System.out.print(a[i] + ",");
}
System.out.println();
//二分查找需要先进性排序
System.out.println("1000位置:"+Arrays.binarySearch(a, ));
} public static void testCopy() {
Random rm=new Random();
int[] a=new int[]; System.out.println("=======测试拷贝======"); for(int i=;i<a.length;i++) {
a[i] = rm.nextInt();
} int[] b=Arrays.copyOf(a, ); //拷贝前6个元素到b中 System.out.println("===拷贝前a===");
for(int i=;i<a.length;i++) {
System.out.print(a[i] + ",");
}
System.out.println(); System.out.println("===拷贝后b===");
for(int i=;i<b.length;i++) {
System.out.print(b[i] + ",");
}
System.out.println();
} public static void testFill() {
int[] a = new int[];
Arrays.fill(a, );
Arrays.fill(a, , , );
System.out.println("===========测试批量赋值============");
System.out.print("数组赋值后:");
for(int i=;i<a.length;i++)
{
System.out.print(a[i] + ",");
}
System.out.println();
} public static void testEquality() {
int[] a = new int[];
Arrays.fill(a, );
int[] b = new int[];
Arrays.fill(b, );
System.out.println(Arrays.equals(a, b)); //数据一样时为true
b[] = ;
System.out.println(Arrays.equals(a, b)); //不一致为false
}
}
    public static void testSort() {
Random rm=new Random();
int[] a=new int[]; System.out.println("=======测试排序======"); for(int i=;i<a.length;i++) {
a[i] = rm.nextInt();
} System.out.println("===排序前===");
for(int i=;i<a.length;i++) {
System.out.print(a[i] + ",");
}
System.out.println(); System.out.println("===排序后===");
Arrays.sort(a); for(int i=;i<a.length;i++) {
System.out.print(a[i] + ",");
}
System.out.println();
}
    public static void SearchSort() {
Random rm=new Random();
int[] a=new int[]; System.out.println("=======测试查找======"); for(int i=;i<a.length;i++) {
a[i] = rm.nextInt();
} a[] = ;
Arrays.sort(a); System.out.println("===查找前===");
for(int i=;i<a.length;i++) {
System.out.print(a[i] + ",");
}
System.out.println();
//二分查找需要先进行排序
System.out.println("1000位置:"+Arrays.binarySearch(a, ));
}
    public static void testCopy() {
Random rm=new Random();
int[] a=new int[]; System.out.println("=======测试拷贝======"); for(int i=;i<a.length;i++) {
a[i] = rm.nextInt();
} int[] b=Arrays.copyOf(a, ); //拷贝前6个元素到b中 System.out.println("===拷贝前a===");
for(int i=;i<a.length;i++) {
System.out.print(a[i] + ",");
}
System.out.println(); System.out.println("===拷贝后b===");
for(int i=;i<b.length;i++) {
System.out.print(b[i] + ",");
}
System.out.println();
}
    public static void testFill() {
int[] a = new int[];
Arrays.fill(a, );
Arrays.fill(a, , , );
System.out.println("===========测试批量赋值============");
System.out.print("数组赋值后:");
for(int i=;i<a.length;i++)
{
System.out.print(a[i] + ",");
}
System.out.println();
}
    public static void testEquality() {
int[] a = new int[];
Arrays.fill(a, );
int[] b = new int[];
Arrays.fill(b, );
System.out.println(Arrays.equals(a, b)); //数据一样时为true
b[] = ;
System.out.println(Arrays.equals(a, b)); //不一致为false
}

三:包装器类Collection

(一)基本方法

JAVA核心技术I---JAVA基础知识(工具类Arrays和Collections类)

处理对象是 Collection及其子类

–排序:对List进行排序,sort。
–搜索:从List中搜索元素,binarySearch
–批量赋值:对List批量赋值,fill。
–最大、最小:查找集合中最大/小值,max,min
–反序:将List 反序排列,reverse

(二)实现

import java.util.ArrayList;
import java.util.Collections; public class CollectionsTest { public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<Integer>();
list.add();
list.add();
list.add();
list.add(); // 排序
Collections.sort(list);
// 检索
System.out.println("元素所在的索引值是:" + Collections.binarySearch(list, ));
//最大最小
System.out.println("最大值:" + Collections.max(list));
System.out.println("最小值:" + Collections.min(list));
Collections.reverse(list); //翻转不需要用到排序 Collections.fill(list, ); //全部赋值为100
}
}

四:对象比较

(一)对象实现Comparable接口(需要修改对象类)

JAVA核心技术I---JAVA基础知识(集合set)

–compareTo方法
> 返回1,==返回0,<返回-
Arrays和Collections在进行对象sort时,自动调用该方法
import java.util.Arrays;

class Dog implements Comparable<Dog>{
int age;
String name; public Dog(String n,int a) {
name=n;
age=a;
} public int getAge() {
return age;
} public String getName() {
return name;
} public int compareTo(Dog another) {
int flag;
flag=name.compareTo(another.getName()); if(flag==) {
return age-another.getAge();
} return flag;
}
} public class CompareToTest {
public static void main(String[] args) {
Dog[] da=new Dog[];
da[]=new Dog("Tom",);
da[]=new Dog("Som",);
da[]=new Dog("Som",); Arrays.sort(da); for(Dog d : da) {
System.out.println(d.getName()+":"+d.getAge());
}
}
}
Som:
Som:
Tom:
    public static void main(String[] args) {
LinkedList<Dog> dl = new LinkedList<Dog>(); dl.add(new Dog("Tom",));
dl.add(new Dog("Som",));
dl.add(new Dog("Som",)); Collections.sort(dl); for(Dog d : dl) {
System.out.println(d.getName()+":"+d.getAge());
}
}

(二)新建Comparator(适用于对象类不可更改的情况)

例如:获取的是class,无法修改源码
–compare方法
> 返回1,==返回0,<返回-
Comparator比较器将作为参数提交给工具类的sort方法
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList; class Dog{  //不可修改,不能添加compareTo,不能继承接口
int age;
String name; public Dog(String n,int a) {
name=n;
age=a;
} public int getAge() {
return age;
} public String getName() {
return name;
}
} class MyComparator implements Comparator<Dog>{  //需要我们自己实现一个比较器
public int compare(Dog one,Dog another) {
int flag;
flag = one.getName().compareTo(another.getName());
if(flag==0) {
return one.getAge()-another.getAge();
}
return flag;
}
}
public class CompareToTest {
public static void main(String[] args) {
LinkedList<Dog> dl = new LinkedList<Dog>(); dl.add(new Dog("Tom",));
dl.add(new Dog("Som",));
dl.add(new Dog("Som",)); Collections.sort(dl,new MyComparator()); for(Dog d : dl) {
System.out.println(d.getName()+":"+d.getAge());
}
}
}
Som:
Som:
Tom:

五:练习

JAVA核心技术I---JAVA基础知识(工具类Arrays和Collections类)

import java.util.Arrays;

import java.util.Scanner;

class Currency {
private String name; //货币名称
private int originalValue; //原始值
private int value; //转换为人民币后的值 public static String[] CURRENCY_NAME = { "CNY", "HKD", "USD", "EUR" }; public static int[] CURRENCY_RATIO = { , , , }; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getOriginalValue() {
return originalValue;
} public void setOriginalValue(int originalValue) {
this.originalValue = originalValue;
} public int getValue() {
return value;
} public void setValue(int value) {
this.value = value;
} } class HKD extends Currency implements Comparable{
// 实现你的构造函数与Comparable中的接口
} class USD extends Currency implements Comparable{
// 实现你的构造函数与Comparable中的接口
} class EUR extends Currency implements Comparable{
// 实现你的构造函数与Comparable中的接口
} public class Main { public static void main(String[] args) {
Currency[] cs = new Currency[];
//初始化 Scanner sc = new Scanner(System.in);
//利用hasNextXXX()判断是否还有下一输入项 int a = ;
int b = ;
int c = ; if (sc.hasNext()) {
a = sc.nextInt();
cs[] = new HKD(a);
} if (sc.hasNext()) {
b = sc.nextInt();
cs[] = new USD(b);
} if (sc.hasNext()) {
c = sc.nextInt();
cs[] = new EUR(c);
}
//初始化结束 //请补充排序 //请补充输出结果 } }

JAVA核心技术I---JAVA基础知识(工具类Arrays和Collections类)

import java.util.Arrays;

import java.util.Scanner;

class Currency {
private String name; //货币名称
private int originalValue; //原始值
private int value; //转换为人民币后的值 public static String[] CURRENCY_NAME = { "CNY", "HKD", "USD", "EUR" }; public static int[] CURRENCY_RATIO = { , , , }; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getOriginalValue() {
return originalValue;
} public void setOriginalValue(int originalValue) {
this.originalValue = originalValue;
} public int getValue() {
return value;
} public void setValue(int value) {
this.value = value;
} } class HKD extends Currency implements Comparable<Currency>{
// 实现你的构造函数与Comparable中的接口
public HKD(int v) {
this.setName(Currency.CURRENCY_NAME[]);
this.setOriginalValue(v);
this.setValue(v*Currency.CURRENCY_RATIO[]/Currency.CURRENCY_RATIO[]); //转为人民币
} public int compareTo(Currency cy) {
return this.getValue()-cy.getValue(); //比较人民币
}
} class USD extends Currency implements Comparable<Currency> {
// 实现你的构造函数与Comparable中的接口
public USD(int v) {
this.setName(Currency.CURRENCY_NAME[]);
this.setOriginalValue(v);
this.setValue(v*Currency.CURRENCY_RATIO[]/Currency.CURRENCY_RATIO[]);
} public int compareTo(Currency cy) {
return this.getValue()-cy.getValue();
}
} class EUR extends Currency implements Comparable<Currency> {
// 实现你的构造函数与Comparable中的接口
public EUR(int v) {
this.setName(Currency.CURRENCY_NAME[]);
this.setOriginalValue(v);
this.setValue(v*Currency.CURRENCY_RATIO[]/Currency.CURRENCY_RATIO[]);
} public int compareTo(Currency cy) {
return this.getValue()-cy.getValue();
}
} public class Main { public static void main(String[] args) {
Currency[] cs = new Currency[];
//初始化 Scanner sc = new Scanner(System.in);
//利用hasNextXXX()判断是否还有下一输入项 int a = ;
int b = ;
int c = ; if (sc.hasNext()) {
a = sc.nextInt();
cs[] = new HKD(a);
} if (sc.hasNext()) {
b = sc.nextInt();
cs[] = new USD(b);
} if (sc.hasNext()) {
c = sc.nextInt();
cs[] = new EUR(c);
}
//初始化结束
//请补充排序
Arrays.sort(cs); //请补充输出结果
for(Currency citem : cs) {
System.out.println(citem.getName()+""+citem.getOriginalValue());
} } }

答案

上一篇:lottie 显示json格式的动画


下一篇:JavaSE常用类之JDK8中Date相关类的常用方法