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 ...
随机推荐
- ACM学习历程—HDU 1276 士兵队列训练问题(队列)
Description 某部队进行新兵队列训练,将新兵从一开始按顺序依次编号,并排成一行横队,训练的规则如下:从头开始一至二报数,凡报到二的出列,剩下的向小序号方向靠 拢,再从头开始进行一至三报数,凡 ...
- Codeforces 758A. Holiday Of Equality 贪心
题目大意: 给定一个长为\(n\)序列,每次操作在一个数上+1,求最小的操作次数使所有的数大小相同. 题解: 对这种题无话可说 #include <cstdio> #include < ...
- 【Lintcode】029.Interleaving String
题目: Given three strings: s1, s2, s3, determine whether s3 is formed by the interleaving of s1 and s2 ...
- 字典树Trie的使用
1. Trie树介绍 Trie,又称单词查找树.前缀树,是一种多叉树结构.如下图所示: 上图是一棵Trie树,表示了关键字集合{“a”, “to”, “tea”, “ted”, “ten”, “i”, ...
- Python中的getattr()函数详解
最近看Dive into python第四章自省中提到getattr()函数,作为一个内建函数平时自己没怎么用过所以也不太理解这个函数的一些用法 看了下函数本身的doc getattr(object, ...
- 【机器学习】随机森林RF
随机森林(RF, RandomForest)包含多个决策树的分类器,并且其输出的类别是由个别树输出的类别的众数而定.通过自助法(boot-strap)重采样技术,不断生成训练样本和测试样本,由训练样本 ...
- 2-1 本章作业&2-2 开发系统与工具选择
2-1 2-2 推荐使用Android Studio开发Flutter
- 抽象方法(abstract method) 和 虚方法 (virtual method), 重载(overload) 和 重写(override)的区别于联系
1. 抽象方法 (abstract method) 在抽象类中,可以存在没有实现的方法,只是该方法必须声明为abstract抽象方法. 在继承此抽象类的类中,通过给方法加上override关键字来实现 ...
- .net core 高吞吐远程方法调用组件XRPC
XRPC的目标非常明确,就是给.net core平台实现一个百万级别的远程方法调用RPC通讯组件.它的设计理念和GRPC一样,基于连接复用的机制实现高吞的性能:XRPC采用了HTTP2复用的思想,在协 ...
- 51nod 1086【背包·DP】
思路: 如果体积乘以数量>=W,那么直接用完全背包模型.如果不到的话,用二进制优化就好了. 基础题,感觉这样写很优雅?回去睡觉. #include <bits/stdc++.h> u ...