Dust Of Dream

知识真的是一个圆么?

ArrayUtils学习笔记

ArrayUtils 拥有以下方法:
toString
将一个数组转换成String,用于打印数组
isEquals
判断两个数组是否相等,采用EqualsBuilder进行判断
toMap
将一个数组转换成Map,如果数组里是Entry则其Key与Value就是新Map的Key和Value,如果是Object[]则Object[0]为KeyObject[1]为Value
clone
拷贝数组
subarray
截取子数组
isSameLength
判断两个数组长度是否相等
getLength
获得数组的长度
isSameType
判段两个数组的类型是否相同
reverse
数组反转
indexOf
查询某个Object在数组中的位置,可以指定起始搜索位置
lastIndexOf
反向查询某个Object在数组中的位置,可以指定起始搜索位置
contains
查询某个Object是否在数组中
toObject
将基本数据类型转换成外包型数据
isEmpty
判断数组是否为空(null和length=0的时候都为空)
addAll
合并两个数组
add
添加一个数据到数组
remove
删除数组中某个位置上的数据
removeElement
删除数组中某个对象(从正序开始搜索,删除第一个)
eg:
        // 1.打印数组
        ArrayUtils.toString(new int[] { 1423 });// {1,4,2,3}
        ArrayUtils.toString(new Integer[] { 1423 });// {1,4,2,3}
        ArrayUtils.toString(null"I'm nothing!");// I'm nothing!

        
// 2.判断两个数组是否相等,采用EqualsBuilder进行判断
        
// 只有当两个数组的数据类型,长度,数值顺序都相同的时候,该方法才会返回True
        
// 2.1 两个数组完全相同
        ArrayUtils.isEquals(new int[] { 123 }, new int[] { 123 });// true
        
// 2.2 数据类型以及长度相同,但各个Index上的数据不是一一对应
        ArrayUtils.isEquals(new int[] { 132 }, new int[] { 123 });// true
        
// 2.3 数组的长度不一致
        ArrayUtils.isEquals(new int[] { 1233 }, new int[] { 123 });// false
        
// 2.4 不同的数据类型
        ArrayUtils.isEquals(new int[] { 123 }, new long[] { 123 });// false
        ArrayUtils.isEquals(new Object[] { 123 }, new Object[] { 1, (long23 });// false
        
// 2.5 Null处理,如果输入的两个数组都为null时候则返回true
        ArrayUtils.isEquals(new int[] { 123 }, null);// false
        ArrayUtils.isEquals(nullnull);// true

        
// 3.将一个数组转换成Map
        
// 如果数组里是Entry则其Key与Value就是新Map的Key和Value,如果是Object[]则Object[0]为KeyObject[1]为Value
        
// 对于Object[]数组里的元素必须是instanceof Object[]或者Entry,即不支持基本数据类型数组
        
// 如:ArrayUtils.toMap(new Object[]{new int[]{1,2},new int[]{3,4}})会出异常
        ArrayUtils.toMap(new Object[] { new Object[] { 12 }, new Object[] { 34 } });// {1=2,
        
// 3=4}
        ArrayUtils.toMap(new Integer[][] { new Integer[] { 12 }, new Integer[] { 34 } });// {1=2,
        
// 3=4}

        
// 4.拷贝数组
        ArrayUtils.clone(new int[] { 324 });// {3,2,4}

        
// 5.截取数组
        ArrayUtils.subarray(new int[] { 34156 }, 24);// {1,5}
        
// 起始index为2(即第三个数据)结束index为4的数组
        ArrayUtils.subarray(new int[] { 34156 }, 210);// {1,5,6}
        
// 如果endIndex大于数组的长度,则取beginIndex之后的所有数据

        
// 6.判断两个数组的长度是否相等
        ArrayUtils.isSameLength(new Integer[] { 135 }, new Long[] { 2L8L10L });// true

        
// 7.获得数组的长度
        ArrayUtils.getLength(new long[] { 1233 });// 3

        
// 8.判段两个数组的类型是否相同
        ArrayUtils.isSameType(new long[] { 13 }, new long[] { 856 });// true
        ArrayUtils.isSameType(new int[] { 13 }, new long[] { 856 });// false

        
// 9.数组反转
        int[] array = new int[] { 125 };
        ArrayUtils.reverse(array);
// {5,2,1}

        
// 10.查询某个Object在数组中的位置,可以指定起始搜索位置,找不到返回-1
        
// 10.1 从正序开始搜索,搜到就返回当前的index否则返回-1
        ArrayUtils.indexOf(new int[] { 136 }, 6);// 2
        ArrayUtils.indexOf(new int[] { 136 }, 2);// -1
        
// 10.2 从逆序开始搜索,搜到就返回当前的index否则返回-1
        ArrayUtils.lastIndexOf(new int[] { 136 }, 6);// 2

        
// 11.查询某个Object是否在数组中
        ArrayUtils.contains(new int[] { 312 }, 1);// true
        
// 对于Object数据是调用该Object.equals方法进行判断
        ArrayUtils.contains(new Object[] { 312 }, 1L);// false

        
// 12.基本数据类型数组与外包型数据类型数组互转
        ArrayUtils.toObject(new int[] { 12 });// new Integer[]{Integer,Integer}
        ArrayUtils.toPrimitive(new Integer[] { new Integer(1), new Integer(2) });// new int[]{1,2}

        
// 13.判断数组是否为空(null和length=0的时候都为空)
        ArrayUtils.isEmpty(new int[0]);// true
        ArrayUtils.isEmpty(new Object[] { null });// false

        
// 14.合并两个数组
        ArrayUtils.addAll(new int[] { 135 }, new int[] { 24 });// {1,3,5,2,4}

        
// 15.添加一个数据到数组
        ArrayUtils.add(new int[] { 135 }, 4);// {1,3,5,4}

        
// 16.删除数组中某个位置上的数据
        ArrayUtils.remove(new int[] { 135 }, 1);// {1,5}

        
// 17.删除数组中某个对象(从正序开始搜索,删除第一个)
        ArrayUtils.removeElement(new int[] { 135 }, 3);// {1,5}

posted on 2008-02-26 18:23 Anemone 阅读(11046) 评论(6)  编辑  收藏 所属分类: 牧羊心得

Feedback

# re: ArrayUtils学习笔记[未登录] 2008-02-26 20:00 eric

pls check out java.util.collection.Arrays  回复  更多评论   

# re: ArrayUtils学习笔记 2008-02-26 22:36 ZelluX

其实觉得这样学效果不好,看了就忘。  回复  更多评论   

# re: ArrayUtils学习笔记 2008-02-27 10:20 Anemone

@ZelluX
我一般习惯于用代码记录心得,我自己有专门的Google项目,里面有自己工作时候遇到的难题或者实现方案的示例代码,发到博客里主要是方便搜索^^  回复  更多评论   

# re: ArrayUtils学习笔记 2008-02-28 15:59 离弦之ray的技术天空

clone是深拷贝还是浅拷贝??  回复  更多评论   

# re: ArrayUtils学习笔记 2009-02-05 09:23 ss

看了就忘记,最高境界。呵呵。  回复  更多评论   

# re: ArrayUtils学习笔记[未登录] 2014-11-20 22:28 xx

@离弦之ray的技术天空
浅,看下源码就知道了  回复  更多评论   



只有注册用户登录后才能发表评论。


网站导航:
 

My Links

Blog Stats

News

常用链接

留言簿(1)

随笔分类

随笔档案

新闻档案

相册

常去网站

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜