ArrayUtils【org.apache.commons.lang3.ArrayUtils】

版权声明:分享知识是一种快乐,愿你我都能共同成长! https://blog.csdn.net/qidasheng2012/article/details/83382796

Maven依赖
本文以3.7版本为例:

<dependency>
	<groupId>org.apache.commons</groupId>
	<artifactId>commons-lang3</artifactId>
	<version>3.7</version>
</dependency>

ArrayUtils 常用API

toString
将一个数组转换成String,用于打印数组

subarray
截取子数组

isSameLength
判断两个数组长度是否相等

getLength
获得数组的长度

isSameType
判段两个数组的类型是否相同

reverse
数组反转

indexOf
查询某个Object在数组中的位置,可以指定起始搜索位置

lastIndexOf
反向查询某个Object在数组中的位置,可以指定起始搜索位置

contains
查询某个Object是否在数组中

toObject
toPrimitive
基本数据类型数组与包装类数据类型数组互转

isEmpty
判断数组是否为空(null和length=0的时候都为空)

addAll
合并两个数组

add
添加一个数据到数组

remove
删除数组中某个位置上的数据

removeElement
删除数组中某个对象(从正序开始搜索,删除第一个)

ArrayUtils 常用API详解

toString
将一个数组转换成String,用于打印数组

ArrayUtils.toString(new int[] { 1, 4, 2, 3 }); // {1,4,2,3}
ArrayUtils.toString(new Integer[] { 1, 4, 2, 3 }); // {1,4,2,3}
ArrayUtils.toString(null, “I’m nothing!”); // I’m nothing!


subarray
截取子数组

ArrayUtils.subarray(new int[] { 3, 4, 1, 5, 6 }, 2, 4); // {1,5}
// 起始index为2(即第三个数据)结束index为4的数组

ArrayUtils.subarray(new int[] { 3, 4, 1, 5, 6 }, 2, 10); // {1,5,6}
// 如果endIndex大于数组的长度,则取beginIndex之后的所有数据


isSameLength
判断两个数组长度是否相等

ArrayUtils.isSameLength(new Integer[] { 1, 3, 5 }, new Long[] { 2L, 8L, 10L });
// true


getLength
获得数组的长度

ArrayUtils.getLength(new long[] { 1, 23, 3 }); // 3


isSameType
判段两个数组的类型是否相同

ArrayUtils.isSameType(new long[] { 1, 3 }, new long[] { 8, 5, 6 }); // true
ArrayUtils.isSameType(new int[] { 1, 3 }, new long[] { 8, 5, 6 }); // false


reverse
数组反转

int[] array = new int[] { 1, 2, 5 };
ArrayUtils.reverse(array); // {5,2,1}


indexOf
查询某个Object在数组中的位置,可以指定起始搜索位置

// 从正序开始搜索,搜到就返回当前的index否则返回-1
ArrayUtils.indexOf(new int[] { 1, 3, 6 }, 6); // 2
ArrayUtils.indexOf(new int[] { 1, 3, 6 }, 2); // -1

lastIndexOf
反向查询某个Object在数组中的位置,可以指定起始搜索位置

// 从逆序开始搜索,搜到就返回当前的index否则返回-1
ArrayUtils.lastIndexOf(new int[] { 1, 3, 6 }, 6); // 2


contains
查询某个Object是否在数组中

ArrayUtils.contains(new int[] { 3, 1, 2 }, 1); // true
// 对于Object数据是调用该Object.equals方法进行判断
ArrayUtils.contains(new Object[] { 3, 1, 2 }, 1L); // false


toObject
toPrimitive
基本数据类型数组与包装类数据类型数组互转

ArrayUtils.toObject(new int[] { 1, 2 }); // new Integer[]{Integer,Integer}
ArrayUtils.toPrimitive(new Integer[] { new Integer(1), new Integer(2) }); // new int[]{1,2}


isEmpty
判断数组是否为空(null和length=0的时候都为空)

ArrayUtils.isEmpty(new int[0]); // true
ArrayUtils.isEmpty(new Object[] { null }); // false


addAll
合并两个数组

ArrayUtils.addAll(new int[] { 1, 3, 5 }, new int[] { 2, 4 }); // {1,3,5,2,4}


add
添加一个数据到数组

ArrayUtils.add(new int[] { 1, 3, 5 }, 4); // {1,3,5,4}


remove
删除数组中某个位置上的数据

ArrayUtils.remove(new int[] { 1, 3, 5 }, 1); // {1,5}

removeElement
删除数组中某个对象(从正序开始搜索,删除第一个)

ArrayUtils.removeElement(new int[] { 1, 3, 5 }, 3); // {1,5}

猜你喜欢

转载自blog.csdn.net/qidasheng2012/article/details/83382796
今日推荐