public class NumberUtilz {
/**
* 检查此字符串是否为数字
*
* @param str
* @return true 如果是数字
*/
public static boolean isNumberic(String str) {
if (str == null || str.equals("")) {
return false;
}
for (int i = 0; i < str.length(); i++) {
if (Character.isDigit(str.charAt(i)) == false) {
return false;
}
}
return true;
}
/**
* <p>
* 检查此字符串是否为正整数
* </p>
*
* <p>
* <code>null</code> will return <code>false</code>. An empty String ("") will
* return <code>false</code>.
* </p>
*
* <pre>
* StringUtils.isPositiveInteger(null) = false
* StringUtils.isPositiveInteger("") = false
* StringUtils.isPositiveInteger(" ") = false
* StringUtils.isPositiveInteger("123") = true
* StringUtils.isPositiveInteger("12 3") = false
* StringUtils.isPositiveInteger("ab2c") = false
* StringUtils.isPositiveInteger("12-3") = false
* StringUtils.isPositiveInteger("12.3") = false
* StringUtils.isPositiveInteger("00") = false
* </pre>
*
* @param str 要检查的字符串,可能是null
* @return <code>true</code> 如果它是正整数的话
*/
public static boolean isPositiveInteger(String str) {
return isNumberic(str) && !(str.startsWith("0") && str.length() > 1);
}
/**
* 检查此字符串是否为小数
*
* @param num
* @return <code>true</code> 如果它是小数的话
*/
public static boolean isDecimal(String num) {
if (num == null || (num.indexOf('.') == -1) || (num.split("\\.").length != 2)) {
return false;
}
String[] str = num.split("\\.");
if (!isPositiveInteger(str[0]) || !isNumberic(str[1])) {
return false;
}
return true;
}
/**
* 去除数组中重复的元素
*
* @param array int数组
* @return 无重复的int数组
*/
public int[] removeDuplicateNumber(int[] array) {
List list = new ArrayList();
for (int i = 0; i < array.length; i++) {
if (!list.contains(new Integer(array[i]))) {
list.add(new Integer(array[i]));
}
}
int[] temp = new int[list.size()];
for (int i = 0; i < list.size(); i++) {
temp[i] = ((Integer) list.get(i)).intValue();
}
return temp;
}
/**
* 冒泡排序
*
* @param x int数组
*/
public static int[] BubbleSort(int[] x) {
for (int i = 0; i < x.length; i++) {
for (int j = i + 1; j < x.length; j++) {
if (x[i] > x[j]) {
int temp = x[i];
x[i] = x[j];
x[j] = temp;
}
}
}
return x;
}
/**
* 选择排序
*
* @param x int数组
*/
public static int[] selectSort(int[] x) {
for (int i = 0; i < x.length; i++) {
int lowerIndex = i;
// 找出最小的一个索引
for (int j = i + 1; j < x.length; j++) {
if (x[j] < x[lowerIndex]) {
lowerIndex = j;
}
}
// 交换
int temp = x[i];
x[i] = x[lowerIndex];
x[lowerIndex] = temp;
}
return x;
}
/**
* 插入排序
*
* @param x int数组
*/
public static int[] insertionSort(int[] x) {
for (int i = 1; i < x.length; i++) {// i从一开始,因为第一个数已经是排好序的啦
for (int j = i; j > 0; j--) {
if (x[j] < x[j - 1]) {
int temp = x[j];
x[j] = x[j - 1];
x[j - 1] = temp;
}
}
}
return x;
}
/**
* 希尔排序
*
* @param x int数组
*/
public static int[] shellSort(int[] x) {
// 分组
for (int increment = x.length / 2; increment > 0; increment /= 2) {
// 每个组内排序
for (int i = increment; i < x.length; i++) {
int temp = x[i];
int j = 0;
for (j = i; j >= increment; j -= increment) {
if (temp < x[j - increment]) {
x[j] = x[j - increment];
} else {
break;
}
}
x[j] = temp;
}
}
return x;
}
/**
* 得到6为随机数
*/
public static int getSixFigures() {
int i = new Random().nextInt(999999);
return i < 100000 ? i + 100000 : i;
}
/**
* 10进制int转换为16进制String
*
* @param dec 10进制数据
* @return String
*/
public static String dec2Hex(int dec) {
StringBuffer sb = new StringBuffer();
sb.append("0x");
for (int i = 0; i < 8; i++) {
int tmp = (dec >> (7 - i % 8) * 4) & 0x0f;
if (tmp < 10)
sb.append(tmp);
else
sb.append((char) ('A' + (tmp - 10)));
}
return sb.toString();
}
/**
* 16进制String转换为10进制int
*
* @param hexStr 16进制数据
* @return int
*/
public static int hex2Dec(String hexStr) {
String myStr[] = { "a", "b", "c", "d", "e", "f" };
int result = 0;
;
int n = 1;
for (int i = hexStr.length() - 1; i >= 0; i--) {
String param = hexStr.substring(i, i + 1);
for (int j = 0; j < myStr.length; j++) {
if (param.equalsIgnoreCase(myStr[j])) {
param = "1" + String.valueOf(j);
}
}
result = Integer.parseInt(param) * n;
n *= 16;
}
return result;
}
/**
* 判断是不是手机号码
*
* @param number
* @return true-是手机号码 false-不是手机号
*/
public static boolean isPhoneNumber(String number) {
Assert.notNull(number, "要检测的手机号不能为空");
Pattern pattern = Pattern
.compile("(^0{0,1}13[0-9]{9}$)|(13\\d{9}$)|(147\\d{8}$)|(15[01235-9]\\d{8}$)|(18[025-9]\\d{8}$)");
Matcher matcher = pattern.matcher(number);
if (matcher.find()) {
return true;
} else {
return false;
}
}
/**
* 得到i位随机数
*
* @param num>0
* @return
*/
public static long getFigures(int num) {
return (long) (Math.random() * 9 * Math.pow(10, num - 1)) + (long) Math.pow(10, num - 1);
}
/**
* 判断字符串是否为数字
*
* @param str
* @return
*/
public static boolean isIntOrDouble(String str) {
Pattern pattern = Pattern.compile("-?[0-9]+.?[0-9]+");
Matcher isNum = pattern.matcher(str);
if (!isNum.matches()) {
return false;
}
return true;
}
}
数字工具类
最新推荐文章于 2022-04-25 16:46:27 发布