读源码Apache-commons-lang3-3.1(一)

 Apache-commons-lang3包中提供了一系列static的方法Utils类。

ArrayUtils类使的数组操作更像String,或者Map的操作一样。


 1.ArrayUtils类的基本方法

EMPTY_OBJECT_ARRAY  : Object[] 长度为0的数组,每一种基本类型都有此对象
toString(Object) 指定对象转化为默认格式的字符串
toString(Object, String) 指定对象转化为指定风格的字符串
isEquals(Object, Object) 判读两个对象是否相相等
toMap(Object[]) 数组对象转化为Map对象
toArray(T...) 动态参数转化为数组



clone(T[]) toPrimitive(Integer[])
nullToEmpty(Object[]) toPrimitive(Integer[], int)
subarray(T[], int, int) toObject(int[])
isSameLength(Object[],  Object[]) isEmpty(Object[])
getLength(Object) isNotEmpty(T[])
isSameType(Object, Object) addAll(T[], T...)
reverse(Object[]) add(T[], T)
indexOf(Object[], Object) add(T[], int, T)
indexOf(Object[], Object, int) remove(T[], int)
lastIndexOf(Object[], Object) removeElement(T[], Object)
lastIndexOf(Object[], Object,  int) removeAll(T[], int...)
contains(Object[], Object) removeElements(T[], T...)

上面例如:subarray,clone,nullToEmpty, indexOf, lastIndexOf更加类似字符串的操作,addAll,add,remove,removeAll,removeElement等像操作Map.

这些方法有大量的重载用来实现不同类型的数组。例如int[]数组重载reverse(int []).

2.ArrayUtils.toMap(Object[] array)方法

public static Map<Object, Object> toMap(Object[] array) {
        if (array == null) {
            return null;
        }
        final Map<Object, Object> map = new HashMap<Object, Object>((int) (array.length * 1.5));
        for (int i = 0; i < array.length; i++) {
            Object object = array[i];
            if (object instanceof Map.Entry<?, ?>) {
                Map.Entry<?,?> entry = (Map.Entry<?,?>) object;
                map.put(entry.getKey(), entry.getValue());
            } else if (object instanceof Object[]) {
                Object[] entry = (Object[]) object;
                if (entry.length < 2) {
                    throw new IllegalArgumentException("Array element " + i + ", '"
                        + object
                        + "', has a length less than 2");
                }
                map.put(entry[0], entry[1]);
            } else {
                throw new IllegalArgumentException("Array element " + i + ", '"
                        + object
                        + "', is neither of type Map.Entry nor an Array");
            }
        }
        return map;
    }

 toMap方法的实现,让二维数组以Key-Value的形式转化为Map,从源代码中的map.put(entry[0],entry[1]);可以看出一个数组元素长度为2,多出部分不作为转化Map的一部分。

 如图下解释:

读源码Apache-commons-lang3-3.1(一)

 测试实例:


Object[][] array = new String[2][2];
        array[0] = new String[] { "k0", "v0", "e0" };
        array[1] = new String[] { "k1", "v1", "e1" };
        Map<Object, Object> map = ArrayUtils.toMap(array);
        for (Object obj : map.keySet()) {
            System.out.println(map.get(obj));
        }

上面使用toMap(Object[])方法的后输出的结果是:v0 \n v1.说明转化Map的时候仅仅截取数组的前两个元素,第一个作为Key,第二个作为Value.

3.关注基本类型数组和包装类型数组之间转换的方法

 基本类型和引用类型之间的转换(AutoBoxing和UnBoxing),即基本类型数组和基本类型对应的引用类型之间的转换

 toPrimitive(Integer[]) : int []

 toPrimitive(Integer[], int) int[];特别说明:int参数是对Integer[]数组中为null的元素的替换值。

 toObject(int[]) Integer[]

 同样,ArrayUtils对基本类型都提供了对象的重载方法。

  ArrayUtils提供的方法使数组的操作更加便捷,功能更加强大,特别是截取,查找,包装等常用方法的实现有很高的重用性。

本文出自 “野马红尘” 博客,谢绝转载!

你可能感兴趣的:(ArrayUtils)