java commons.lang3 ArrayUtils使用
java commons.lang3 ArrayUtils使用
import org.apache.commons.lang3.ArrayUtils;
/**
*数组追加数组,不重复
*/
public static int[] arrayAddArray(int[] src,int[] arr) {
// 查询某个Object是否在数组中
// ArrayUtils.contains(new int[] { 3, 1, 2 }, 1);// true
int[] newarr = ArrayUtils.clone(src);
for (int i = 0; i < arr.length; i++) {
if( !ArrayUtils.contains(newarr,arr[i]) ){
newarr = ArrayUtils.add(newarr, arr[i]);
}
}
// System.out.println("---------------");
// for (int i = 0; i < newarr.length; i++) {
// System.out.println(newarr[i]);
// }
return newarr;
}
/**
*取数组在一个大小范围内的值
*/
public static int[] getArea(int[] src,int[] minmaxNum) {
int slen = src.length;
int[] newarr = {};
for (int i = 0; i < slen; i++) {
if(src[i]>=minmaxNum[0] && src[i]<=minmaxNum[1]){
newarr = ArrayUtils.add(newarr, src[i]);
}
}
// System.out.println("---------------");
// for (int i = 0; i < newarr.length; i++) {
// System.out.println(newarr[i]);
// }
// ArrayUtils.reverse(newarr);//数组反转
return newarr;
}
============================
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
删除数组中某个对象(从正序开始搜索,删除第一个)
// 1.打印数组
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!
// 2.判断两个数组是否相等,采用EqualsBuilder进行判断
// 只有当两个数组的数据类型,长度,数值顺序都相同的时候,该方法才会返回True
// 2.1 两个数组完全相同
ArrayUtils.isEquals(new int[] { 1, 2, 3 }, new int[] { 1, 2, 3 });// true
// 2.2 数据类型以及长度相同,但各个Index上的数据不是一一对应
ArrayUtils.isEquals(new int[] { 1, 3, 2 }, new int[] { 1, 2, 3 });// true
// 2.3 数组的长度不一致
ArrayUtils.isEquals(new int[] { 1, 2, 3, 3 }, new int[] { 1, 2, 3 });// false
// 2.4 不同的数据类型
ArrayUtils.isEquals(new int[] { 1, 2, 3 }, new long[] { 1, 2, 3 });// false
ArrayUtils.isEquals(new Object[] { 1, 2, 3 }, new Object[] { 1, (long) 2, 3 });// false
// 2.5 Null处理,如果输入的两个数组都为null时候则返回true
ArrayUtils.isEquals(new int[] { 1, 2, 3 }, null);// false
ArrayUtils.isEquals(null, null);// 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[] { 1, 2 }, new Object[] { 3, 4 } });// {1=2,
// 3=4}
ArrayUtils.toMap(new Integer[][] { new Integer[] { 1, 2 }, new Integer[] { 3, 4 } });// {1=2,
// 3=4}
// 4.拷贝数组
ArrayUtils.clone(new int[] { 3, 2, 4 });// {3,2,4}
// 5.截取数组
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之后的所有数据
// 6.判断两个数组的长度是否相等
ArrayUtils.isSameLength(new Integer[] { 1, 3, 5 }, new Long[] { 2L, 8L, 10L });// true
// 7.获得数组的长度
ArrayUtils.getLength(new long[] { 1, 23, 3 });// 3
// 8.判段两个数组的类型是否相同
ArrayUtils.isSameType(new long[] { 1, 3 }, new long[] { 8, 5, 6 });// true
ArrayUtils.isSameType(new int[] { 1, 3 }, new long[] { 8, 5, 6 });// false
// 9.数组反转
int[] array = new int[] { 1, 2, 5 };
ArrayUtils.reverse(array);// {5,2,1}
// 10.查询某个Object在数组中的位置,可以指定起始搜索位置,找不到返回-1
// 10.1 从正序开始搜索,搜到就返回当前的index否则返回-1
ArrayUtils.indexOf(new int[] { 1, 3, 6 }, 6);// 2
ArrayUtils.indexOf(new int[] { 1, 3, 6 }, 2);// -1
// 10.2 从逆序开始搜索,搜到就返回当前的index否则返回-1
ArrayUtils.lastIndexOf(new int[] { 1, 3, 6 }, 6);// 2
// 11.查询某个Object是否在数组中
ArrayUtils.contains(new int[] { 3, 1, 2 }, 1);// true
// 对于Object数据是调用该Object.equals方法进行判断
ArrayUtils.contains(new Object[] { 3, 1, 2 }, 1L);// false
// 12.基本数据类型数组与外包型数据类型数组互转
ArrayUtils.toObject(new int[] { 1, 2 });// 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[] { 1, 3, 5 }, new int[] { 2, 4 });// {1,3,5,2,4}
// 15.添加一个数据到数组
ArrayUtils.add(new int[] { 1, 3, 5 }, 4);// {1,3,5,4}
// 16.删除数组中某个位置上的数据
ArrayUtils.remove(new int[] { 1, 3, 5 }, 1);// {1,5}
// 17.删除数组中某个对象(从正序开始搜索,删除第一个)
ArrayUtils.removeElement(new int[] { 1, 3, 5 }, 3);// {1,5}
java commons.lang3 ArrayUtils使用的更多相关文章
- org.apache.commons.lang3.ArrayUtils 学习笔记
package com.nihaorz.model; /** * @作者 王睿 * @时间 2016-5-17 上午10:05:17 * */ public class Person { privat ...
- 【java】org.apache.commons.lang3功能示例
org.apache.commons.lang3功能示例 package com.simple.test; import java.util.Date; import java.util.Iterat ...
- spring异常记录-----java.lang.NoClassDefFoundError: org/apache/commons/lang3/StringUtils
今天在练习怎样SSH中进行单元測试的时候出现下列异常: SEVERE: Exception starting filter Struts2 java.lang.NoClassDefFoundError ...
- Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang3.StringUtils
1.错误叙述性说明 2014-7-10 23:12:23 org.apache.catalina.core.StandardContext filterStart 严重: Exception star ...
- Hadoop java.lang.ClassNotFoundException: org.apache.commons.lang3.StringUtils
.jar 学习好友推荐案例的时候,提交运行时报错找不到StringUtils java.lang.ClassNotFoundException: org.apache.commons.lang3.St ...
- struts2中的错误--java.lang.NoClassDefFoundError: org/apache/commons/lang3/StringUtils
2013-4-7 10:13:56 org.apache.catalina.startup.HostConfig checkResources 信息: Reloading context [/chap ...
- org.apache.commons.lang3.tuple.Pair 作为更新参数,XML 中的 Sql 取不到值、报错
项目用的 Mybatis,今天改一个需求,落地实现是批量更新,且只需要根据主键(id)来更新一个字段(name). 于是,没有犹豫,像下面这样设计了数据结构: 既然是批量更新,那外层肯定是 List ...
- NoClassDefFoundError: org/apache/commons/lang3/StringUtils
出错信息: 2014-2-5 21:38:05 org.apache.catalina.core.StandardContext filterStart严重: Exception starting f ...
- ERROR----java.lang.NoClassDefFoundError: org/apache/commons/lang3/StringUtils
2013-4-28 13:17:57 org.apache.catalina.core.StandardContext filterStart 严重: Exception starting filte ...
随机推荐
- 【AngularJs-模块篇-Form篇】
1.模块 <!doctype html> <html lang="en-US" ng-app="myApp"> <head> ...
- Des加密(js+java结果一致)【原创】
des加密算法,javascript版本和java版本 目录: 1.资源文件下载 2.JavaScript文件(des.js) 3.html文件(des.html) 4.java文件(des.java ...
- C++/C, Java学习资料
C++11系列-什么是C++11 [Java]Final 与 C++ Const的区别 C++开发者都应该使用的10个C++11特性 史上最明白的 NULL.0.nullptr 区别分析 C语言堆栈入 ...
- 利用 :first-child 和 :nth-last-child 确定子元素数目
读<css揭秘>时,发现选择器的神奇作用,可以确定子元素数目,比如: li:first-child:nth-last-child(2),li:first-child:nth-last-ch ...
- Python排列函数:sort、sorted
排序函数介绍:sort()和sorted()都属于Python list的排序方法 区别:sort()属于永久性排列,直接改变该list: sorted属于暂时性排列,会产生一个新的序列. #sort ...
- are not called implicitly
php.net <?php class BaseClass{ function __construct() { print "In BaseClass constructor<b ...
- 2018/04/04 PHP 中的 数组排序问题
简单说一下为什么要总结一下PHP中关于数组排序的问题. 在很多时候我们对于数组要进行排序,但是 PHP 中对于排序已经有了很多封装. 但是某些函数是修改原数组,一部分是返回修改后数组,与其一个个试,不 ...
- 正则表达式(三):Unicode诸问题下篇(转)
原文:http://www.infoq.com/cn/news/2011/04/regular-expressions-4 我们使用正则表达式,熟练掌握各种功能和结构只是手段,解决实际的问题才是真正的 ...
- javaScript高级教程(八)-----正则表达式温故知新
1.RegExp对象:五个属性二个方法 五个属性:global, ignoreCase,multiline,lastIndex,source 二个方法: exec()--模式匹配 test()--检测 ...
- 交换机-查看mac地址表
1.使用交换机命令行 en 或者 enable 进入特权模式 Switch> Switch>en Switch# Switch# 2.查看交换机中的MAC地址表 Switch#sh ...