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[] { 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 });//false
        // 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}

from:http://langgufu.iteye.com/blog/2107430

Apache commons lang3包ArrayUtils工具使用相关推荐

  1. Apache commons lang3 StringUtils工具类

    Apache commons lang3 StringUtils工具类 Apache commons lang3包下的StringUtils工具类中封装了一些字符串操作的方法,非常实用,使用起来也非常 ...

  2. 工具类org.apache.commons.lang3.StringUtils

    sEmpty 和 isBlank 的区别你知道吗?也许你除了isEmpty/isNotEmpty/isNotBlank/isBlank外,并不知道还有isAnyEmpty/isNoneEmpty/is ...

  3. java的StringUtils.isBlank和StringUtils.isEmpty方法区别(org.apache.commons.lang3.StringUtils)

    前言 估计很多朋友跟我一样,平时也不会特别去注意究竟用isBlank还是isEmpty去判断空字符串,但是大部分场景优先使用isBlank就对了. isEmpty是否为空,只有当==null或者==& ...

  4. Java中字符串工具类继承org.apache.commons.lang3.StringUtils类代码

    场景 转换为字节数组:是否包含字符串:替换掉HTML标签方法:替换为手机识别的HTML,去掉样式及属性,保留回车: 缩略字符串(不区分中英文字符):转换为Double类型:转换为Float类型:转换为 ...

  5. org.apache.commons.lang3.ArrayUtils 学习笔记

    package com.nihaorz.model;/*** @作者 王睿* @时间 2016-5-17 上午10:05:17**/ public class Person {private Stri ...

  6. org.apache.commons.lang3.StringUtils 的相关用法

    一.jar包下载 commons-lang3-3.1.jar java 开发工具commons-lang3-3.0 jar包,有org.apache.commons.lang3.StringUtils ...

  7. 错误: java.lang.ClassNotFoundException: org.apache.commons.lang3.StringUtils

    做项目的时候,实现图片异步上传并返回json数据,但是图片上传成功,json数据没有返回,报错:  java.lang.ClassNotFoundException: org.apache.commo ...

  8. 【apache lang3】import org.apache.commons.lang3.StringUtils error----Cannot resolve symbol ‘lang3‘

    问题:Cannot resolve symbol 'lang3' 在spring boot 使用StringUtils时,发现import出现问题,解析不了lang3 原因: 找到依赖配置文件 imp ...

  9. Spark - Illegal pattern component: XXX 与org.apache.commons.lang3.time.FastDateFormat incompatible

    一.引言 使用 sparkSession 读文件时出现 java.lang.IllegalArgumentException: Illegal pattern component: XXX 报错,解决 ...

最新文章

  1. 【原创】关于部门月会(二)
  2. django_2.0_请求处理
  3. 得到课程《组织行为学》学习笔记07
  4. java的编译和连接方法_Java:编译时解析和“最具体的方法”
  5. matlab dividend,[原创]基于(Matlab/R/C++)的方差Gamma模型(Hull期权期货)随机抽样[by fantuanxiaot]...
  6. 【干货】人人都能看懂的LSTM
  7. 采用HttpModules来重写URLS
  8. MiniDao Framework 1.3.0 发布,J2EE持久化解决方案
  9. 探索 Word 2007 开发(四):上传图片
  10. 同一html页面手机pc不同显示,Nginx根据手机端与电脑端设备相同地址显示不同页面内容...
  11. Centos7做回收站功能,防止误删除
  12. 金蝶KIS专业版V14.1下载链接,金蝶KIS专业版V14.1新增功能介绍 安装包下载地址
  13. Win10应用商店被卸载的恢复方法
  14. nc 自开节点做参照_NC6period;3常见问题记录
  15. C++ 使用chrono库准确统计代码运行时间
  16. gee引擎修改UI界面图文教程
  17. wps/excel 正则表达式 提取数字
  18. 汇编语言里 eax, ebx, ecx, edx, esi, edi, ebp, esp 寄存器 含义
  19. 小身躯,大志向,宇通智能巴士有哪些黑科技
  20. Hadoop官网翻译 (HDFS命令)

热门文章

  1. myqltransactionRollbackexception deadlock found when trying to get lock
  2. mysql 变量set
  3. 命令行修改weblogic用户名和密码
  4. How to allow/block PING on Linux server – IPTables rules for icmp---reference
  5. 【采用】知识图谱简介及风控应用场景
  6. CML 2020 | 显式引入对分类标签的描述,如何提高文本分类的效果?
  7. 大象转身,地表最强投行高盛开启转型之路
  8. Philip S. Yu 讲的广度学习到底是什么?
  9. Spring5源码 - Spring IOC 注解复习
  10. 计算机网络基础 单选题) 作业,南开大学《计算机网络基础》在线作业及答案