- 浏览: 196852 次
- 性别:
- 来自: 南京
文章分类
最新评论
-
xyyx_nk:
在demo中没有下载的示例,能详细说一下怎么监听下载过程吗?比 ...
DhNet 网络http工具(带缓存功能哦) -dhroid文档 -
zhaoguowei998:
dhroid ioc模块对 加密混淆问题 -
zhaoguowei998:
你太牛了,佩服佩服,以后要多想你请教了
dhroid ioc模块对 加密混淆问题 -
白色蜻蜓:
转载下您的文章,已注明出处。
IOS开发之百度地图API应用 -
wenjiefeng:
你好,在andrioidpn-client客户端源码里,Not ...
Android 推送之原理与初触Androidpn(Android Push Notification)
数组是我们经常需要使用到的一种数据结构,但是由于Java本身并没有提供很好的API支持,使得很多操作实际上做起来相当繁琐,以至于我们实际编码中甚至会不惜牺牲性能去使用Collections API,用Collection当然能够很方便的解决我们的问题,但是我们一定要以性能为代价吗?ArrayUtils帮我们解决了处理类似情况的大部分问题。来看一个例子:
package sean.study.jakarta.commons.lang;
import java.util.Map;
import org.apache.commons.lang.ArrayUtils;
public class ArrayUtilsUsage {
public static void main(String[] args) {
// data setup
int[] intArray1 = { 2, 4, 8, 16 };
int[][] intArray2 = { { 1, 2 }, { 2, 4 }, { 3, 8 }, { 4, 16 } };
Object[][] notAMap = {
{ "A", new Double(100) },
{ "B", new Double(80) },
{ "C", new Double(60) },
{ "D", new Double(40) },
{ "E", new Double(20) }
};
// printing arrays
System.out.println("intArray1: " + ArrayUtils.toString(intArray1));
System.out.println("intArray2: " + ArrayUtils.toString(intArray2));
System.out.println("notAMap: " + ArrayUtils.toString(notAMap));
// finding items
System.out.println("intArray1 contains '8'? "
+ ArrayUtils.contains(intArray1, 8));
System.out.println("intArray1 index of '8'? "
+ ArrayUtils.indexOf(intArray1, 8));
System.out.println("intArray1 last index of '8'? "
+ ArrayUtils.lastIndexOf(intArray1, 8));
// cloning and resversing
int[] intArray3 = ArrayUtils.clone(intArray1);
System.out.println("intArray3: " + ArrayUtils.toString(intArray3));
ArrayUtils.reverse(intArray3);
System.out.println("intArray3 reversed: "
+ ArrayUtils.toString(intArray3));
// primitive to Object array
Integer[] integerArray1 = ArrayUtils.toObject(intArray1);
System.out.println("integerArray1: "
+ ArrayUtils.toString(integerArray1));
// build Map from two dimensional array
Map map = ArrayUtils.toMap(notAMap);
Double res = (Double) map.get("C");
System.out.println("get 'C' from map: " + res);
}
}
以下是运行结果:
intArray1: {2,4,8,16}
intArray2: {{1,2},{2,4},{3,8},{4,16}}
notAMap: {{A,100.0},{B,80.0},{C,60.0},{D,40.0},{E,20.0}}
intArray1 contains '8'? true
intArray1 index of '8'? 2
intArray1 last index of '8'? 2
intArray3: {2,4,8,16}
intArray3 reversed: {16,8,4,2}
integerArray1: {2,4,8,16}
get 'C' from map: 60.0
这段代码说明了我们可以如何方便的利用ArrayUtils类帮我们完成数组的打印、查找、克隆、倒序、以及值型/对象数组之间的转换等操作。如果想了解更多,请参考Javadoc。
处理文本对Java应用来说应该算是家常便饭了,在1.4出现之前,Java自身提供的API非常有限,如String、StringTokenizer、StringBuffer,操作也比较单一。无非就是查找substring、分解、合并等等。到1.4的出现可以说Java的文字处理上了一个台阶,因为它支持regular expression了。这可是个重量级而方便的东东啊,缺点是太复杂,学习起来有一定难度。相较而言,Jakarta Commons提供的StringUtils和WordUtils至今还维持着那种简洁而强大的美,使用起来也很顺手。来看一个例子:
package sean.study.jakarta.commons.lang;
import org.apache.commons.lang.StringUtils;
public class StringUtilsAndWordUtilsUsage {
public static void main(String[] args) {
// data setup
String str1 = "";
String str2 = " ";
String str3 = "\t";
String str4 = null;
String str5 = "123";
String str6 = "ABCDEFG";
String str7 = "It feels good to use Jakarta Commons.\r\n";
// check for empty strings
System.out.println("==============================");
System.out.println("Is str1 blank? " + StringUtils.isBlank(str1));
System.out.println("Is str2 blank? " + StringUtils.isBlank(str2));
System.out.println("Is str3 blank? " + StringUtils.isBlank(str3));
System.out.println("Is str4 blank? " + StringUtils.isBlank(str4));
// check for numerics
System.out.println("==============================");
System.out.println("Is str5 numeric? " + StringUtils.isNumeric(str5));
System.out.println("Is str6 numeric? " + StringUtils.isNumeric(str6));
// reverse strings / whole words
System.out.println("==============================");
System.out.println("str6: " + str6);
System.out.println("str6 reversed: " + StringUtils.reverse(str6));
System.out.println("str7: " + str7);
String str8 = StringUtils.chomp(str7);
str8 = StringUtils.reverseDelimited(str8, ' ');
System.out.println("str7 reversed whole words : \r\n" + str8);
// build header (useful to print log messages that are easy to locate)
System.out.println("==============================");
System.out.println("print header:");
String padding = StringUtils.repeat("=", 50);
String msg = StringUtils.center(" Customised Header ", 50, "%");
Object[] raw = new Object[]{padding, msg, padding};
String header = StringUtils.join(raw, "\r\n");
System.out.println(header);
}
}
输出的结果如下:
==============================
Is str1 blank? true
Is str2 blank? true
Is str3 blank? true
Is str4 blank? true
==============================
Is str5 numeric? true
Is str6 numeric? false
==============================
str6: ABCDEFG
str6 reversed: GFEDCBA
str7: It feels good to use Jakarta Commons.
str7 reversed whole words :
Commons. Jakarta use to good feels It
==============================
print header:
==================================================
%%%%%%%%%%%%%%% Customised Header %%%%%%%%%%%%%%%%
==================================================
从代码中我们可以大致了解到这个StringUtils类简单而强大的处理能力,从检查空串(对null的情况处理很得体),到分割子串,到生成格式化的字符串,使用都很简洁,也很直截了当。
发表评论
-
webservice 一些在线接口
2012-09-09 20:39 811webservice工厂接口 http://www.36 ... -
StringUtils 工具类的常用方法
2012-09-04 12:06 797以下是StringUtils的各项用法1.空字符串检查使 ... -
Java之WeakReference与SoftReference使用讲解
2012-08-22 09:46 634如果你想写一个 Java 程序,观察某对象什么时候会被垃圾收集 ... -
Commons Collections Transforme
2012-08-14 16:02 629接下来看Transformer组 ... -
Commons Collections - Predicate
2012-08-14 16:03 745接下来看Predicate组 Predi ... -
Commons Collections - Comparator组
2012-08-14 16:04 865接下来我们会讲到辅助类,首先看Comparator组。 ... -
[转]Commons Collections - Map组
2012-08-13 11:52 768接下来看Map组。 BidiMap Multi ... -
commons-collections Bag Buffer
2012-08-13 11:38 622Bag HashBag BagUtils B ... -
Commons Collections 学习1
2012-08-13 11:28 777Commons Collections,又是一个重量级的 ... -
IO包学习笔记
2012-08-13 11:22 799IO包学习笔记 IO几 ... -
commons-lang
2012-08-13 10:36 809我们首先来看org.apache.commons.lan ... -
apache commons beanutils
2012-07-12 12:47 702/* * UserBean * 所含属 ... -
commons-BeanUtils简介
2012-07-12 12:01 912Beanutils用了魔术般的反射技术,实现了很多夸张有 ... -
Prototyping Tools(产品原型设计工具)
2012-06-23 17:56 12871、Axure Axure RP Pro是一个产品经理必备的交 ... -
应用eclipse生成doc(解决"编码GBK的不可映射字符"问题)
2012-06-21 21:50 696步骤:新建工程,将源码放到src中,右键项目-export—— ... -
浅谈Java对象的强、软、弱和虚引用(转载)
2012-06-19 14:33 499在JDK1.2以前的版 ... -
java.lang.NoSuchFieldError: deferredExpression解决
2012-06-11 17:54 909java.lang.NoSuchFieldError: de ... -
ExecutorService 线程池线程
2011-10-06 15:42 1122ExecutorService 建立多线程 ... -
用pull解析和生成xml简单实例
2011-09-29 18:39 1051pull 解析也是事件驱动的,android中已经 ... -
javaio
2011-09-11 01:32 954Java中比较丰富的I/O操作 ...
相关推荐
META-INF/MANIFEST.MFMETA-INF/LICENSE.txtMETA-INF/NOTICE.txtorg.apache.commons.lang.ArrayUtils.class org.apache.commons.lang.BitField.class org.apache.commons.lang.BooleanUtils.class org.apache....
META-INF/MANIFEST.MFMETA-INF/LICENSE.txtMETA-INF/NOTICE.txtorg.apache.commons.lang.ArrayUtils.class org.apache.commons.lang.BitField.class org.apache.commons.lang.BooleanUtils.class org.apache....
需要先 import org.apache.commons.lang3.StringUtils; /* 内含 common-lang3.jar commons-lang3-3.9-bin.zip commons-lang3-3.9-src.zip 使用说明.txt*/ public static String getClientIp(HttpServletRequest ...
org.apache.commons.lang3.StringUtils.jar,打开是common-lang3.jar文件,以及使用方法,根据使用方法使用即可,绝对可靠
包含有org.apache.commons.lang3.StringUtils; org.apache.commons.lang3.reflect.FieldUtils等类
org.apache.commons.lang3.ArrayUtils.class org.apache.commons.lang3.BitField.class org.apache.commons.lang3.CharUtils.class org.apache.commons.lang3.ClassUtils.class org.apache.commons.lang3.Validate....
org.apache.commons.lang所有包的集合,
org.apache.commons.lang jar包
commons.lang ArrayUtils类的中文api 主要扩展了对java.lang包下对数组的操作!
Apache Commons Lang资源包,下载解压缩后,可获得api文档,源码,jar包,用于开发
org.apache.commons.lang3_3.1.0.v201403281430.jar 工具类
com.springsource.org.apache.commons.lang
commons-lang3-3.1 StringUtils字符串jar包 org.apache.commons.lang3.StringUtils的jar包
import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.HashCodeBuilder;
解决json数据中,返回的数据格式中带有反斜杠 如下所示{\"Count\":\"3\",\"ErrorString\":\"\",\"Success\":true,\"URL\":\"http:\\\/\\\/172.16.80.65:8080\\\/LoginSSO.aspx?UserCode=wubg&Type=WorkItem\",\...
META-INF/MANIFEST.MFMETA-INF/LICENSE.txtMETA-INF/NOTICE.txtorg.apache.commons.lang.ArrayUtils.class org.apache.commons.lang.BitField.class org.apache.commons.lang.BooleanUtils.class org.apache.commons...
jar包,官方版本,自测可用
apache.commons.lang3
jar包,官方版本,自测可用
整个系统都没有错误,但是在发布运行后,控制台上却抛出了这样的异常: java.lang.ClassNotFoundException: org.apache.commons.dbcp.BasicDataSource