Arrays工具类中的toString/binarySearch/sort/asList示例代码
2017-05-10 11:05
411 查看
****************************************测试类**************************************** package com.**.equalsDemo; import java.util.Arrays; import java.util.Date; import java.util.List; /** * @author Ckinghan * @描述:Arrays工具类的常用方法 */ public class ArraysDemo { public static void main(String[] args) { Integer[] integers = {1,3,35,32,3,2,34,23,42,234,223,34,234,24,34}; /** * Arrays工具类的asList方法,将数组转换为list集合 */ List<Integer> list = Arrays.asList(integers); for(Integer integer : list){ System.out.println("数组通过Arrays.asList转换为list集合输出结果:"+integer); } System.out.println("------------------------------------------------我是分隔线------------------------------------------------"); /** * Arrays.sort可以对数组进行自然排序(从大到小) */ Arrays.sort(integers); /** * Arrays.toString可以将数组转换为String字符串,注意,它返回的值与数组.toString返回的值不同, * 数组.toString返回的值是一个hashCode地址 * Arrays.toString返回的是数组的值 */ String arrayString = Arrays.toString(integers); System.out.println("对数组进行自然排序后的结果并使用Arrays.toString转换后的结果为:"+arrayString); System.out.println("数组integers.toString返回的值是:"+integers.toString()); System.out.println("------------------------------------------------我是分隔线------------------------------------------------"); /** * Arrays.binarySearch可以查找数组中值存在的索引位置 * 查找的方式为折半查询(也称为二分查找法,叫法不同,都是一个意思) * 这种查找的方式速度很快,但有个前提,数组必须是有序的。 */ Integer valIndex = Arrays.binarySearch(integers, 35); System.out.println("通过Arrays.binarySearch折半算法查询到的索引位置为:"+valIndex); System.out.println("------------------------------------------------我是分隔线------------------------------------------------"); /** * 注意:如果自己创建的类想使用Arrays.sort排序时,必须实现Compareble接口中的comparaTo接口 */ Entity[] entities = {new Entity(1, "employeeInfo", new Date()), new Entity(2, "employeeInfo", new Date()), new Entity(3, "employeeInfo", new Date()), new Entity(1, "employeeInfo", new Date())}; Arrays.sort(entities); for(Entity entity : entities){ System.out.println(entity); } } } ****************************************实体类**************************************** package com.**.equalsDemo; import java.util.Date; public class Entity implements Comparable<Entity>{ public Integer id; public String tableName; public Date createTime; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getTableName() { return tableName; } public void setTableName(String tableName) { this.tableName = tableName; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } @Override public String toString() { return "entity [id=" + id + ", tableName=" + tableName + ", createTime=" + createTime + "]"; } public Entity(Integer id, String tableName, Date createTime) { super(); this.id = id; this.tableName = tableName; this.createTime = createTime; } public Entity() { super(); } /** * 如果想在set集合中,对比存放对象的具体内容,须要重写equals */ public boolean equals(Object obj) { if(obj instanceof Entity){ Entity entity = (Entity)obj; if(entity.id == this.id && this.tableName != null && this.tableName.equals(entity.tableName) && (this.createTime.compareTo(entity.getCreateTime())) == 0){ return true; } } return false; } /** * 还须要重写hashCode,因为equals在判断是否相等时,还会判断hashCode */ public int hashCode() { return 100000; } /** * 如果想使用Arrays中的sort排序,必须重写compareTo方法,否则会报错 */ @Override public int compareTo(Entity o) { Integer val = this.id - o.id; if(val == 0 && this.tableName != null){ val = this.tableName.compareTo(o.tableName); if(val == 0){ val = this.createTime.compareTo(o.createTime); } } return val; } } ****************************************执行结果**************************************** 数组通过Arrays.asList转换为list集合输出结果:1 数组通过Arrays.asList转换为list集合输出结果:3 数组通过Arrays.asList转换为list集合输出结果:35 数组通过Arrays.asList转换为list集合输出结果:32 数组通过Arrays.asList转换为list集合输出结果:3 数组通过Arrays.asList转换为list集合输出结果:2 数组通过Arrays.asList转换为list集合输出结果:34 数组通过Arrays.asList转换为list集合输出结果:23 数组通过Arrays.asList转换为list集合输出结果:42 数组通过Arrays.asList转换为list集合输出结果:234 数组通过Arrays.asList转换为list集合输出结果:223 数组通过Arrays.asList转换为list集合输出结果:34 数组通过Arrays.asList转换为list集合输出结果:234 数组通过Arrays.asList转换为list集合输出结果:24 数组通过Arrays.asList转换为list集合输出结果:34 ------------------------------------------------我是分隔线------------------------------------------------ 对数组进行自然排序后的结果并使用Arrays.toString转换后的结果为:[1, 2, 3, 3, 23, 24, 32, 34, 34, 34, 35, 42, 223, 234, 234] 数组integers.toString返回的值是:[Ljava.lang.Integer;@4c76ec2d ------------------------------------------------我是分隔线------------------------------------------------ 通过Arrays.binarySearch折半算法查询到的索引位置为:10 ------------------------------------------------我是分隔线------------------------------------------------ entity [id=1, tableName=employeeInfo, createTime=Wed May 10 11:00:45 CST 2017] entity [id=1, tableName=employeeInfo, createTime=Wed May 10 11:00:45 CST 2017] entity [id=2, tableName=employeeInfo, createTime=Wed May 10 11:00:45 CST 2017] entity [id=3, tableName=employeeInfo, createTime=Wed May 10 11:00:45 CST 2017]
相关文章推荐
- Java集合框架-4.【集合工具类:Collections、Arrays】【JDK1.5新特性】
- [疯狂Java]数组:Arrays(数组工具类)
- Java集合框架:Arrays工具类
- java的Arrays工具类实战
- (79)Arrays工具类:集合或者普通数组的操作方法、集合和数组相互转换
- Collections和Arrays工具类
- 在Java中Arrays工具类实现功能的六种方法
- java:数组操作工具类 java.util.Arrays包 主要方法详解
- day02--Java中数组工具类:Arrays
- 万能的数组工具类:Arrays
- java集合(6)——数组工具类Arrays
- Java基础之集合框架--Arrays工具类(专门操作数组的工具类)
- 黑马程序员_Java第17天知识总结_集合框架的工具类_Collections_Arrays_将数组变成list集合_集合变数组_高级for循环_可变参数
- Java基础知识强化之集合框架笔记33:Arrays工具类中asList()方法的使用
- Java基础--工具类(Collections、Arrays)及其他常用类
- Java 8增强的工具类 Arrays(二)
- 3.8学习内容-常用的Arrays工具类方法和冒泡排序
- Arrays数组工具类
- 黑马程序员_JAVA集合框架的工具类:Collections、Arrays
- 集合框架工具类 Collections ,Arrays