Java 数字数组随机数工具类 NumberUtils、ArrayUtils、RandomUtils用法
commons-lang3-3-3.8.1
//-----------------------------------------------------------------------
/**
* <p>Checks whether the <code>String</code> contains only
* digit characters.</p>
*
* <p><code>Null</code> and empty String will return
* <code>false</code>.</p>
*
* @param str the <code>String</code> to check
* @return <code>true</code> if str contains only Unicode numeric
*/
public static boolean isDigits(final String str) {
return StringUtils.isNumeric(str);
}
/**
* <p>Checks if the CharSequence contains only Unicode digits.
* A decimal point is not a Unicode digit and returns false.</p>
*
* <p>{@code null} will return {@code false}.
* An empty CharSequence (length()=0) will return {@code false}.</p>
*
* <p>Note that the method does not allow for a leading sign, either positive or negative.
* Also, if a String passes the numeric test, it may still generate a NumberFormatException
* when parsed by Integer.parseInt or Long.parseLong, e.g. if the value is outside the range
* for int or long respectively.</p>
*
* <pre>
* StringUtils.isNumeric(null) = false
* StringUtils.isNumeric("") = false
* StringUtils.isNumeric(" ") = false
* StringUtils.isNumeric("123") = true
* StringUtils.isNumeric("\u0967\u0968\u0969") = true
* StringUtils.isNumeric("12 3") = false
* StringUtils.isNumeric("ab2c") = false
* StringUtils.isNumeric("12-3") = false
* StringUtils.isNumeric("12.3") = false
* StringUtils.isNumeric("-123") = false
* StringUtils.isNumeric("+123") = false
* </pre>
*
* @param cs the CharSequence to check, may be null
* @return {@code true} if only contains digits, and is non-null
* @since 3.0 Changed signature from isNumeric(String) to isNumeric(CharSequence)
* @since 3.0 Changed "" to return false and not true
*/
public static boolean isNumeric(final CharSequence cs) {
if (isEmpty(cs)) {
return false;
}
final int sz = cs.length();
for (int i = 0; i < sz; i++) {
if (!Character.isDigit(cs.charAt(i))) {
return false;
}
}
return true;
}
一、NumberUtils工具类
org.apache.commons.lang3.math.NumberUtils
/1.NumberUtils.isNumber():判断字符串是否是数字/
NumberUtils.isNumber("5.96");//结果是true
NumberUtils.isNumber("s5");//结果是false
NumberUtils.isNumber("0000000000596");//结果是true
/2.NumberUtils.isDigits():判断字符串中是否全为数字/
NumberUtils.isDigits("0000000000.596");//false
NumberUtils.isDigits("0000000000596");//true
/3.NumberUtils.toInt():字符串转换为整数/
NumberUtils.toInt("5");
NumberUtils.toLong("5");
NumberUtils.toByte("3");
NumberUtils.toFloat("3.2");
NumberUtils.toDouble("4");
NumberUtils.toShort("3");
/4.NumberUtils.max():找出最大的一个/
NumberUtils.max(newint[]{3,5,6});//结果是6
NumberUtils.max(3,1,7);//结果是7
/5.NumberUtils.min():找出最小的一个/
NumberUtils.min(newint[]{3,5,6});//结果是6
NumberUtils.min(3,1,7);//结果是7
/6.NumberUtils.createBigDecimal()通过字符串创建BigDecimal类型,支持long、int、float、double、number等数值/
NumberUtils.createBigDecimal("1");
NumberUtils.createLong("1");
NumberUtils.createInteger("1");
二、ArrayUtils工具类
/1.ArrayUtils.isEmpty(strs):判断数组是否为空, 不为空返回false, 为空true/
ArrayUtils.isEmpty(new String[]{"21","是"});//结果是false
ArrayUtils.isEmpty(new String[]{""});//结果是false
ArrayUtils.isEmpty(new String[]{null});//结果是false
ArrayUtils.isEmpty(new String[]{});//结果是true
/2.ArrayUtils.isNotEmpty(strs):判断数组是否不为空,不为空返回true,为空false/
ArrayUtils.isNotEmpty(new String[]{"21","是"});//结果是true
ArrayUtils.isNotEmpty(new String[]{""});//结果是true
ArrayUtils.isNotEmpty(new String[]{});//结果是false
/3.ArrayUtils.isSameLength(strs,strs2):判断两个数组长度是否相等,长度相等返回true,否则返回false。相比较的两个数组类型必须相同/
ArrayUtils.isSameLength(new String[]{"21","是"},new String[]{"21","是"});//返回false
/4.ArrayUtils.isSameType(strs,strs2):判断两个数组的类型是否相同,相同返回true,否则返回false/
ArrayUtils.isSameType(new String[]{"21","是"},newInteger[]{3});
/5.ArrayUtils.isEquals(strs,strs2)判断两个数组是否相等/
ArrayUtils.isEquals(strs,strs);//结果是true
/6.ArrayUtils.toString()将一个数组转换成String,用于打印/
ArrayUtils.toString(new String[]{"21","是"});//结果是:{21,是}
/7.ArrayUtils.clone赋值(克隆)数组/
Object[]s=ArrayUtils.clone(newObject[]{"33","yy"});
/8.ArrayUtils.subarray截取子数组:根据起始索引startIndexInclusive到结束索引startIndexInclusive/
Object[]s1=ArrayUtils.subarray(newObject[]{"33","yy","uu"},0,1);//结果是返回数组:[33]
Object[]s2=ArrayUtils.subarray(newObject[]{"33","yy","uu"},0,2);//结果是返回数组:[33,yy]
/9.ArrayUtils.indexOf查询某个object在数组中的位置,可是指定起始搜索位置/
intindex=ArrayUtils.indexOf(newObject[]{"33","yy","uu"},"uu");//结果是2
intindex1=ArrayUtils.indexOf(newObject[]{"33","yy","uu"},"uu",2);//结果是2
intindex3=ArrayUtils.indexOf(newObject[]{"33","yy","uu"},"uu",3);//结果是-1
/10.ArrayUtils.lastIndexOf反向查询某个object在数组中的位置,可以指定起始搜索位置/
intindex11=ArrayUtils.lastIndexOf(newObject[]{"33","yy","uu"},"33");//结果是0
intindex22=ArrayUtils.lastIndexOf(newObject[]{"33","yy","uu"},"33",2);
/11.ArrayUtils.contains查询某个object是否在数组中/
ArrayUtils.contains(new String[]{"1", "2", "3"}, "11");
/12.ArrayUtils.reverse反转数组/
ArrayUtils.reverse(new String[]{"22","yy"});//结果是:{"yy","22"}
/13.ArrayUtils.add添加一object到数组/
String[] t={"22","yy"};
String[] gg=(String[])ArrayUtils.add(t,"jj");//{"22","yy","jj"}
/14.ArrayUtils.addAll合并两个数组/
String[]ggo=(String[])ArrayUtils.addAll(new String[]{"22","yy"},new String[]{"jj"});//结果是:[22,yy,jj]
ArrayUtils.addAll(new String[]{"22","yy"},new String[]{"jj", "jj"}); //结果是:[22,yy,jj,jj]
/15.ArrayUtils.remove删除数组某个位置的元素/
String[]gg4=(String[])ArrayUtils.remove(new String[]{"22","yy"},1);
/16.ArrayUtils.removeElement删除数组中某个对象/
String[]ggpp=(String[])ArrayUtils.removeElement(new String[]{"22","yy"},"yy");
三、RandomUtils工具类
RandomUtils帮助我们产生随机数,不止是数字类型,连boolean类型都可以通过RandomUtils产生,RandomStringUtils生成字符随机数。
RandomUtils.nextBoolean();
RandomUtils.nextDouble();
RandomUtils.nextLong();
// 注意这里传入的参数不是随机种子,而是在0~1000之间产生一位随机数
RandomUtils.nextInt(1000);
https://segmentfault.com/a/1190000004922657
Java 数字数组随机数工具类 NumberUtils、ArrayUtils、RandomUtils用法的更多相关文章
- java操作数组的工具类-Arrays
static int binarySearch(type[] a, type key) 使用二分搜索法来搜索key元素在数组中的索引:若a数组不包括key,返回负数.(该方法必须已按升序排列后调用). ...
- Java学习关于随机数工具类--Random类
Random类是伪随机数生成器.之所以称为伪随机数(pseudorandom),是因为它们只是简单的均匀分布序列.Random类定义了以下构造函数: Random() Random(long seed ...
- 【Java】字节数组转换工具类
import org.apache.commons.lang.ArrayUtils; import java.nio.charset.Charset; /** * 字节数组转换工具类 */ publi ...
- java.util.Arrays----操作数组的工具类
java.util.Arrays操作数组的工具类,里面定义了很多操作数组的方法 1.boolean equals(int[] a,int[] b):判断两个数组是否相等. 2.String toStr ...
- Java学习-041-颜色工具类(RGB,HEX)
在日常的网页开发中,经常需要进行颜色数值获取.转换,例如获取红色,获取蓝色,获取绿色,RGB转十六进制颜色,十六进制颜色转RGB等,因而在学习过程中,写了一个小工具类,仅供各位小主参考! 多不闲言,直 ...
- Java容器---Arrays & Collections工具类
1.Array & Arrays 与Collection & Collections区别 (1)Collection": 是一个接口,与其子类共同组成一个Collection ...
- Java日期时间实用工具类
Java日期时间实用工具类 1.Date (java.util.Date) Date(); 以当前时间构造一个Date对象 Date(long); 构造函数 ...
- Java线程的并发工具类
Java线程的并发工具类. 一.fork/join 1. Fork-Join原理 在必要的情况下,将一个大任务,拆分(fork)成若干个小任务,然后再将一个个小任务的结果进行汇总(join). 适用场 ...
- Rhino+envjs-1.2.js 在java运行网站js 工具类
java爬虫遇到个页面加密的东西,找了些资料学习学习 做了个java运行js的工具类,希望对大家有用,其中用到client(获取js)可以自行换成自己的client.主要是用了 Rhino就是Java ...
随机推荐
- 基于OpenCV的面部交换
需要装python库 OpenCV dlib docopt(根据打开方式选择是否装) # -*- coding: UTF-8 #本电脑试运行 命令 python F:\python_project\s ...
- 【Lintcode】096.Partition List
题目: Given a linked list and a value x, partition it such that all nodes less than x come before node ...
- 变废为宝,用旧电脑自己DIY组建 NAS 服务器
i17986 出品,必属佳作! 前言: 老外不喜欢升级硬件和软件,大家应该都知道.我昨天无意看到 FreeNAS 自述文件,这个系统可以让你使用旧的计算机硬件,于是我决定这么做.垃圾电脑你怎么能没有, ...
- python爬虫知识点总结(五)正则表达式
在线正则表达式匹配:http://tool.oschina.net/regex 正则表达式学习:https://c.runoob.com/front-end/854 一.什么是正则表达式? 常见匹配模 ...
- 百度地图API的第一次接触——自定义控件
1.定义一个控件类,即function function ZoomControl(){ // 设置默认停靠位置和偏移量 this.defaultAnchor = BMAP_ANCHOR_TOP_LEF ...
- MySQL锁之一:锁详解
一. 什么是死锁 死锁是指两个或两个以上的进程在执行过程中,因争夺资源而造成的一种互相等待的现象,若无外力作用,它们都将无法推进下去.此时称系统处于死锁状态或系统产生了死锁,这些永远在互相等的进 ...
- node url
var url = require("url") url模块提供的三个方法: url.parse(urlStr[, parseQueryString][, slashesDenot ...
- linux下c语言利用iconv函数实现utf-8转unicode
iconv是linux下的编码转换的工具,它提供命令行的使用和函数接口支持 man手册iconv命令用法如下: iconv -f encoding -t encoding inputfile 有如下选 ...
- 32.Docker安装MongoDb
从hub.docker.com上去找镜像 阿里云的国内的镜像地址 填上去之后,然后重启下docker就可以了 docker images列出本地的镜像 拉取mango的镜像 运行这个镜像 docker ...
- 【转载】Eclipse:Android开发中如何查看System.out.println的输出内容
Android开发中在代码中通过System.out.println的输出内容不知道去哪了,在console视图中看不到.而通过Log.i之类的要在Logcat视图中看到,夹杂了太多的其它App及底层 ...