您的位置:首页 > 其它

Commons lang3 包ArrayUtils类使用

2015-08-10 14:32 295 查看
[java] view
plaincopy

package com.aimilin.test;

import java.util.List;

import java.util.Map;

import org.apache.commons.lang3.ArrayUtils;

import org.junit.Test;

public class LangTest {

// 打印数组

public static <T> void p(T... obj) {

if (obj == null)

return;

for (T t : obj) {

System.out.println(t);

}

}

// 打印列表

public static <T> void p(List<T> list) {

if (list == null)

return;

for (T t : list) {

System.out.println(t);

}

}

// 打印Map

public static void p(Map<?, ?> map) {

if (map == null)

return;

for (Object key : map.keySet()) {

p("key:" + key + "\tvalue:" + map.get(key));

}

}

@Test

public void testArrayUtils() {

p("=============== ArrayUtils 工具类使用 ,这个类对null的输入处理很好================");

String strs[] = { "str1", "str2", "str3" };

p(ArrayUtils.add(strs, 0, "测试字符串"));

p("向数组中末尾添加元素");

p(ArrayUtils.add(strs, "str3"));

p("向数组指定位置添加元素");

p(ArrayUtils.add(strs, 1, "str3"));

p("向数组中添加所有元素");

p(ArrayUtils.addAll(strs, "str3", "str4"));

p("拷贝数组");

p(ArrayUtils.clone(strs));

p("数组中是否包含指定的元素");

p(ArrayUtils.contains(strs, "str2"));

p("获取数组的长度");

p(ArrayUtils.getLength(strs));

p("查找第一次出现的位置");

p(ArrayUtils.indexOf(strs, "str2"));

p("判断数组是否是空的");

p(ArrayUtils.isEmpty(strs));

p("判断数组是否非空");

p(ArrayUtils.isNotEmpty(strs));

p("判断数组是否相等");

p(ArrayUtils.isEquals(strs, new String[] { "str2" }));

p("判断数组长度是否相等");

p(ArrayUtils.isSameLength(strs, strs));

p("判断数组类型是否相同");

p(ArrayUtils.isSameType(strs, new String[] { "str" }));

p("查找数组中最后出现元素的位置");

p(ArrayUtils.lastIndexOf(strs, "str2"));

p("null转换成空数组");

p(ArrayUtils.nullToEmpty(strs));

p("从数组中移除指定索引的元素");

p(ArrayUtils.remove(strs, 1));

p("从数组中移除所有指定的元素");

p(ArrayUtils.removeAll(strs, 1, 2));

p("移除数组中指定的元素");

p(ArrayUtils.removeElement(strs, "str1"));

p("移除数组中指定的所有元素");

p(ArrayUtils.removeElements(strs, "str1", "str2"));

p("颠倒数组");

ArrayUtils.reverse(strs);

p(strs);

p("子数组");

p(ArrayUtils.subarray(strs, 0, 2));

p("构建数组");

p(ArrayUtils.toArray("str1", "str2"));

p("将二维数组转换成Map");

String mapStr[][] = { { "key1", "value1" }, { "key2", "value2" } };

p(ArrayUtils.toMap(mapStr));

p("将基本类型数组转换成包装类型");

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

p("将包装类型数组转换成基本类型");

p(ArrayUtils.toPrimitive(new Integer[] { 1, 2, 3, 4, 5 }));

p("将数组用字符串的形式输出");

p(ArrayUtils.toString(strs));

}

}

测试结果:

[java] view
plaincopy

=============== ArrayUtils 工具类使用 ,这个类对null的输入处理很好================

测试字符串

str1

str2

str3

向数组中末尾添加元素

str1

str2

str3

str3

向数组指定位置添加元素

str1

str3

str2

str3

向数组中添加所有元素

str1

str2

str3

str3

str4

拷贝数组

str1

str2

str3

数组中是否包含指定的元素

true

获取数组的长度

3

查找第一次出现的位置

1

判断数组是否是空的

false

判断数组是否非空

true

判断数组是否相等

false

判断数组长度是否相等

true

判断数组类型是否相同

true

查找数组中最后出现元素的位置

1

null转换成空数组

str1

str2

str3

从数组中移除指定索引的元素

str1

str3

从数组中移除所有指定的元素

str1

移除数组中指定的元素

str2

str3

移除数组中指定的所有元素

str3

颠倒数组

str3

str2

str1

子数组

str3

str2

构建数组

str1

str2

将二维数组转换成Map

key:key2 value:value2

key:key1 value:value1

将基本类型数组转换成包装类型

1

2

3

4

5

将包装类型数组转换成基本类型

[I@95c083

将数组用字符串的形式输出

{str3,str2,str1}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: