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 ...
随机推荐
- 使用google浏览器模拟手机终端的方法
谷歌Chrome浏览器,可以很方便地用来当移动终端模拟器.在Windows的[开始]-->[运行]中输入以下命令,启动谷歌浏览器,即可模拟相应手机的浏览器去访问3G手机网页,前提:将先前开启的谷 ...
- Xcode 中的main.m
在几乎所有的程序开发中程序一般都是从main函数开始运行的,那么IOS程序也不例外,在上图中我们可以看到Xcode为我们生成了一个main.m文件: // // main.m // iOS // // ...
- poj1417 True Liars[并查集+背包]
有一点小转化的题,在设计dp状态时还是有点费脑筋的. 地址. 依题意,首先可以知道肯定要扩展域的并查集(明摆着的嘛).一个"好人"域,一个"坏人"域,每句话分两 ...
- 第十二章: 部署Django
本章包含创建一个django程序最必不可少的步骤 在服务器上部署它 如果你一直跟着我们的例子做,你可能正在用runserver 但是runserver 要部署你的django程序,你需要挂接到工业用的 ...
- ASP.NET Core会议管理平台实战_3、认证、授权表迁移
可以参考老张的这个文章: https://www.cnblogs.com/laozhang-is-phi/p/10660403.html 创建这个类库 看一下IdentityUser是在哪一个库下面. ...
- Redis使用的相关问题
Redis用那些数据结构? 字符串类型String 字典Hash 列表List 集合Set 有序集合SortedSet HyperLogLog.Geo.Pub/Sub Redis Module.Blo ...
- hdu 3853 LOOPS (概率dp 逆推求期望)
题目链接 LOOPS Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 125536/65536 K (Java/Others)Tota ...
- 牛客多校3 A-PACM Team(状压降维+路径背包)
PACM Team 链接:https://www.nowcoder.com/acm/contest/141/A来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144 ...
- Python包管理工具小结
此文已由作者张耕源授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 作为一名接触Python有一段时间的初学者,越来越体会到Python的方便之处,它使人能更 多的关注业务本身 ...
- 使用ASP.NET Core实现Docker的HealthCheck指令
写在前面 HealthCheck 不仅是对应用程序内运行情况.数据流通情况进行检查, 还包括应用程序对外部服务或依赖资源的健康检查. 健康检查通常是以暴露应用程序的HTTP端点的形式 实施,可用于配 ...