2007-07-19
妙用Commons良药<四>
关键字: 基础
妙用Commons良药<四>
谈一谈Math包的一点内容,和怎样对属性文件,xml文件进行访问
1、关于org.apache.commons.lang.math的应用
可使用Commons Lang的NumberUtils.max()和NumberUtils.min()方法来从基本类型数组(如
当然,你可以处理数学的范围,需要为某个变量定义一组可接受的值,并检测该变量值是否落在指定的范围内.Range是一个接口,定义了一个简单的数值范围,它有几个不同的实现,如NumberRange,DoubleRange,FloatRange,IntRange以及LongRange等等.
如果你想生成一个随机数,可以这样写:
其作用相当于:int randomInt = (int) Math.floor( (Math.random( ) * (double) maxVal) );
此外提示一点,RandomUtils.nextBoolean()可以返回一个随机的boolean变量
2、关于Jakarta Commons Math包一点应用
举一个例子,如下:
当然,这一个包也可以处理复数,矩阵,统计以及线性方程,具体的用法参考官方的文档
3、使用Commons Configuration包配置应用程序
如果想访问属性文件的话,可以运用PropertiesConfiguration这一个类:
属性文件如下:
speed=23.332
names=Bob,Gautam,Jarret,Stefan
correct=false
如果想访问一个xml文件里面具体的属性,可以使用XMLConfiguration的一个实现从某个文档中加载配置参数。举例如下:
其配置文件:
具体访问的代码如下:
谈一谈Math包的一点内容,和怎样对属性文件,xml文件进行访问
1、关于org.apache.commons.lang.math的应用
可使用Commons Lang的NumberUtils.max()和NumberUtils.min()方法来从基本类型数组(如
double[],float[],long[],int[],short[],byte[])中检索出最小或最大值.举例:
import org.apache.commons.lang.math.NumberUtils;
double[] array = {0.2, 0.4, 0.5, -3.0, 4.223, 4.226};
double max = NumberUtils.max( array ); // returns 4.226
double min = NumberUtils.min( array ); // returns -3.0
当然,你可以处理数学的范围,需要为某个变量定义一组可接受的值,并检测该变量值是否落在指定的范围内.Range是一个接口,定义了一个简单的数值范围,它有几个不同的实现,如NumberRange,DoubleRange,FloatRange,IntRange以及LongRange等等.
import org.apache.commons.lang.math.DoubleRange;
import org.apache.commons.lang.math.Range;
Range safeSpeed = new DoubleRange( 0.0, 65.0 );
double currentSpeed = getCurrentSpeed( );
if( !safeSpeed.containsDouble( currentSpeed ) ) {
System.out.println( "Warning, current speed is unsafe." );
}
如果你想生成一个随机数,可以这样写:
import org.apache.commons.lang.math.RandomUtils; // Create a random integer between 0 and 30 int maxVal = 30; int randomInt = RandomUtils.nextInt( maxVal );
其作用相当于:int randomInt = (int) Math.floor( (Math.random( ) * (double) maxVal) );
此外提示一点,RandomUtils.nextBoolean()可以返回一个随机的boolean变量
2、关于Jakarta Commons Math包一点应用
举一个例子,如下:
import org.apache.commons.math.stat.StatUtils;
double[] values = new double[] { 2.3, 5.4, 6.2, 7.3, 23.3 };
System.out.println( "min: " + StatUtils.min( values ) );
System.out.println( "max: " + StatUtils.max( values ) );
System.out.println( "平均值: " + StatUtils.mean( values ) );
System.out.println( "积: " + StatUtils.product( values ) );
System.out.println( "和: " + StatUtils.sum( values ) );
System.out.println( "方差: " + StatUtils.variance( values ) );
当然,这一个包也可以处理复数,矩阵,统计以及线性方程,具体的用法参考官方的文档
3、使用Commons Configuration包配置应用程序
如果想访问属性文件的话,可以运用PropertiesConfiguration这一个类:
属性文件如下:
speed=23.332
names=Bob,Gautam,Jarret,Stefan
correct=false
其代码如下:
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.PropertiesConfiguration;
Configuration config = new PropertiesConfiguration( "test.properties" );
float speed = config.getFloat("speed"));
List names = config.getList("names"));
boolean correct = config.getBoolean("correct");
如果想访问一个xml文件里面具体的属性,可以使用XMLConfiguration的一个实现从某个文档中加载配置参数。举例如下:
其配置文件:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<engine-config>
<start-criteria>
<criteria type="critical">
Temperature Above -10 Celsius
</criteria>
<criteria> Fuel tank is not empty </criteria>
</start-criteria>
<name>
<first>Tom</first>
<last>Payne</last>
</name>
<horsepower>42</horsepower>
</engine-config>
具体访问的代码如下:
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.DOMConfiguration;
String resource = "com/discursive/jccook/configuration/global.xml";
Configuration config = new DOMConfiguration(resource);
// Retrieve a list of all Criteria elements
List startCriteria = config.getList("start-criteria.criteria");
// Retrieve the value of the first criteria element
String firstCriteria = config.getString("start-criteria.criteria(0)");
// Retrieve the type attribute of the first criteria element
String firstCriteriaType = config.getString("start-criteria.criteria(0)[@type]");
// Retrieve the horsepower as an int
int horsepower = config.getInt("horsepower");
评论
lighter
2007-11-17
我的MSN是:wu_gc@hotmail.com
huozhe
2007-11-15
好贴,受益了,感谢LZ共享!
同时还看了LZ写的中文分词,我最近正准备学习下关于lucene的全文检索,希望能与LZ交流一下~
我的msn:chenwei27@hotmail.com
能否留个联系方式~
同时还看了LZ写的中文分词,我最近正准备学习下关于lucene的全文检索,希望能与LZ交流一下~
我的msn:chenwei27@hotmail.com
能否留个联系方式~
发表评论
- 浏览: 239753 次
- 性别:

- 来自: 广州

- 详细资料
搜索本博客
我的相册
7
共 27 张
共 27 张
最新评论
-
url传递中文参数笔录
结论很恶心啊!有时为了直观些,给别人的链接就需要是中文的啊!
-- by qdzheng -
[转载] 无所不能的“蚂蚁 ...
牛人一个,鉴定完毕!
-- by citi.sh -
关于ClassLoader工作机制 ...
访问量:236752 次 做个极好,哈哈
-- by citi.sh -
经常上的几个网站
挺好的
-- by baby2080 -
interrupt方法点滴记录
try{ Thread.sleep(10000); } catch (In ...
-- by baby2080






评论排行榜