public class CommonBaseCode {
/**
* 判断任意一个整数是否素数
*
* @param n
* @return boolean
*/
public static boolean isPrimes(int n) {
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
/**
* 获得任意一个整数的阶乘,递归
*
* @param n
* @return n!
*/
public static int factorial(int n) {
if (n == 1) {
return 1;
}
return n * factorial(n - 1);
}
/**
* 将指定byte数组以16进制的形式打印到控制台
*
* @param hint
* String
* @param b
* byte[]
* @return void
*/
public static void printHexString(String hint, byte[] b) {
System.out.print(hint);
for (int i = 0; i < b.length; i++) {
String hex = Integer.toHexString(b[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
System.out.print(hex.toUpperCase() + " ");
}
System.out.println("");
}
/**
* 对给定的字符数组进行字典排序
*
* @param chs
* 目标字符数组
* @param upperFisrt
* 大写字母是否在前
* @return 排序后的字符数组
*/
public static char[] sortChars(char[] chs, final boolean upperFisrt) {
Character[] srcArray = new Character[chs.length];
char[] retArray = new char[chs.length];
int index = 0;
for (char ch : chs) {
srcArray[index++] = ch;
}
Arrays.sort(srcArray, new Comparator <Character>() {
public int compare(Character c1, Character c2) {
char ch1 = Character.toUpperCase(c1);
char ch2 = Character.toUpperCase(c2);