一:工具类
–不存储数据,而是在数据容器上,实现高效操作
• 排序
• 搜索
–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[10];
System.out.println("=======测试排序======");
for(int i=0;i<a.length;i++) {
a[i] = rm.nextInt();
}
System.out.println("===排序前===");
for(int i=0;i<a.length;i++) {
System.out.print(a[i] + ",");
}
System.out.println();
System.out.println("===排序后===");
Arrays.sort(a);
for(int i=0;i<a.length;i++) {
System.out.print(a[i] + ",");
}
System.out.println();
}
public static void SearchSort() {
Random rm=new Random();
int[] a=new int[10];
System.out.println("=======测试查找======");
for(int i=0;i<a.length;i++) {
a[i] = rm.nextInt();
}
a[8] = 1000;
Arrays.sort(a);
System.out.println("===查找前===");
for(int i=0;i<a.length;i++) {
System.out.print(a[i] + ",");
}
System.out.println();
//二分查找需要先进性排序
System.out.println("1000位置:"+Arrays.binarySearch(a, 1000));
}
public static void testCopy() {
Random rm=new Random();
int[] a=new int[10];
System.out.println("=======测试拷贝======");
for(int i=0;i<a.length;i++) {
a[i] = rm.nextInt();
}
int[] b=Arrays.copyOf(a, 6); //拷贝前6个元素到b中
System.out.println("===拷贝前a===");
for(int i=0;i<a.length;i++) {
System.out.print(a[i] + ",");
}
System.out.println();
System.out.println("===拷贝后b===");
for(int i=0;i<b.length;i++) {
System.out.print(b[i] + ",");
}
System.out.println();
}
public static void testFill() {
int[] a = new int[10];
Arrays.fill(a, 100);
Arrays.fill(a, 2, 8, 200);
System.out.println("===========测试批量赋值============");
System.out.print("数组赋值后:");
for(int i=0;i<a.length;i++)
{
System.out.print(a[i] + ",");
}
System.out.println();
}
public static void testEquality() {
int[] a = new int[10];
Arrays.fill(a, 100);
int[] b = new int[10];
Arrays.fill(b, 100);
System.out.println(Arrays.equals(a, b)); //数据一样时为true
b[9] = 200;
System.out.println(Arrays.equals(a, b)); //不一致为false
}
}
View Code
public static void testSort() {
Random rm=new Random();
int[] a=new int[10];
System.out.println("=======测试排序======");
for(int i=0;i<a.length;i++) {
a[i] = rm.nextInt();
}
System.out.println("===排序前===");
for(int i=0;i<a.length;i++) {
System.out.print(a[i] + ",");
}
System.out.println();
System.out.println("===排序后===");
Arrays.sort(a);
for(int i=0;i<a.length;i++) {
System.out.print(a[i] + ",");
}
System.out.println();
}
public static void SearchSort() {
Random rm=new Random();
int[] a=new int[10];
System.out.println("=======测试查找======");
for(int i=0;i<a.length;i++) {
a[i] = rm.nextInt();
}
a[8] = 1000;
Arrays.sort(a);
System.out.println("===查找前===");
for(int i=0;i<a.length;i++) {
System.out.print(a[i] + ",");
}
System.out.println();
//二分查找需要先进行排序
System.out.println("1000位置:"+Arrays.binarySearch(a, 1000));
}
public static void testCopy() {
Random rm=new Random();
int[] a=new int[10];
System.out.println("=======测试拷贝======");
for(int i=0;i<a.length;i++) {
a[i] = rm.nextInt();
}
int[] b=Arrays.copyOf(a, 6); //拷贝前6个元素到b中
System.out.println("===拷贝前a===");
for(int i=0;i<a.length;i++) {
System.out.print(a[i] + ",");
}
System.out.println();
System.out.println("===拷贝后b===");
for(int i=0;i<b.length;i++) {
System.out.print(b[i] + ",");
}
System.out.println();
}
public static void testFill() {
int[] a = new int[10];
Arrays.fill(a, 100);
Arrays.fill(a, 2, 8, 200);
System.out.println("===========测试批量赋值============");
System.out.print("数组赋值后:");
for(int i=0;i<a.length;i++)
{
System.out.print(a[i] + ",");
}
System.out.println();
}
public static void testEquality() {
int[] a = new int[10];
Arrays.fill(a, 100);
int[] b = new int[10];
Arrays.fill(b, 100);
System.out.println(Arrays.equals(a, b)); //数据一样时为true
b[9] = 200;
System.out.println(Arrays.equals(a, b)); //不一致为false
}
三:包装器类Collection
(一)基本方法
处理对象是 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(1);
list.add(12);
list.add(2);
list.add(19);
// 排序
Collections.sort(list);
// 检索
System.out.println("元素所在的索引值是:" + Collections.binarySearch(list, 12));
//最大最小
System.out.println("最大值:" + Collections.max(list));
System.out.println("最小值:" + Collections.min(list));
Collections.reverse(list); //翻转不需要用到排序
Collections.fill(list, 100); //全部赋值为100
}
}
四:对象比较
(一)对象实现Comparable接口(需要修改对象类)
–compareTo方法
> 返回1,==返回0,<返回-1
–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==0) {
return age-another.getAge();
}
return flag;
}
}
public class CompareToTest {
public static void main(String[] args) {
Dog[] da=new Dog[3];
da[0]=new Dog("Tom",13);
da[1]=new Dog("Som",27);
da[2]=new Dog("Som",16);
Arrays.sort(da);
for(Dog d : da) {
System.out.println(d.getName()+":"+d.getAge());
}
}
}
public static void main(String[] args) {
LinkedList<Dog> dl = new LinkedList<Dog>();
dl.add(new Dog("Tom",13));
dl.add(new Dog("Som",27));
dl.add(new Dog("Som",16));
Collections.sort(dl);
for(Dog d : dl) {
System.out.println(d.getName()+":"+d.getAge());
}
}
(二)新建Comparator(适用于对象类不可更改的情况)
–compare方法
> 返回1,==返回0,<返回-1
–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",13));
dl.add(new Dog("Som",27));
dl.add(new Dog("Som",16));
Collections.sort(dl,new MyComparator());
for(Dog d : dl) {
System.out.println(d.getName()+":"+d.getAge());
}
}
}
五:练习
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 = { 100, 118, 15, 13 };
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[3];
//初始化
Scanner sc = new Scanner(System.in);
//利用hasNextXXX()判断是否还有下一输入项
int a = 0;
int b = 0;
int c = 0;
if (sc.hasNext()) {
a = sc.nextInt();
cs[0] = new HKD(a);
}
if (sc.hasNext()) {
b = sc.nextInt();
cs[1] = new USD(b);
}
if (sc.hasNext()) {
c = sc.nextInt();
cs[2] = new EUR(c);
}
//初始化结束
//请补充排序
//请补充输出结果
}
}
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 = { 100, 118, 15, 13 };
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[1]);
this.setOriginalValue(v);
this.setValue(v*Currency.CURRENCY_RATIO[0]/Currency.CURRENCY_RATIO[1]); //转为人民币
}
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[2]);
this.setOriginalValue(v);
this.setValue(v*Currency.CURRENCY_RATIO[0]/Currency.CURRENCY_RATIO[2]);
}
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[3]);
this.setOriginalValue(v);
this.setValue(v*Currency.CURRENCY_RATIO[0]/Currency.CURRENCY_RATIO[3]);
}
public int compareTo(Currency cy) {
return this.getValue()-cy.getValue();
}
}
public class Main {
public static void main(String[] args) {
Currency[] cs = new Currency[3];
//初始化
Scanner sc = new Scanner(System.in);
//利用hasNextXXX()判断是否还有下一输入项
int a = 0;
int b = 0;
int c = 0;
if (sc.hasNext()) {
a = sc.nextInt();
cs[0] = new HKD(a);
}
if (sc.hasNext()) {
b = sc.nextInt();
cs[1] = new USD(b);
}
if (sc.hasNext()) {
c = sc.nextInt();
cs[2] = new EUR(c);
}
//初始化结束
//请补充排序
Arrays.sort(cs);
//请补充输出结果
for(Currency citem : cs) {
System.out.println(citem.getName()+""+citem.getOriginalValue());
}
}
}
答案