/**
* 长度填充
*/
public void StringFormat() {
String.format("%5d", 123);//输出" 123" =>右侧对齐,左侧不足5位的的地方补空格
String.format("%-5d", 123);//输出"123 " => 左侧对齐,右侧不足5位的地方用空格补齐
}
/**
* 进制转换
*/
public static void radioConversion() {
//二进制转十进制
String binaryVal = "0000010010001110";
int decVal = Integer.parseInt(binaryVal, 2);
//十进制转十六进制
String hex16 = Integer.toHexString(decVal);
//十六进制转十进制
String hex16Val = "48E";
Integer dec10Val = Integer.parseInt(hex16Val, 16);
//十进制转二进制
String binVal = Integer.toBinaryString(dec10Val);
}
/**
* ASCII码转换相关
*/
public static void AsciiEx () {
//字符转ASCII码
char ch = 'A';
System.out.println("字符转ASCII码Val " + (int) ch);
//ASCII码转字符
int code = 98;
char codeC = (char) code;
System.out.println("Ascii转字符 " + codeC);
//数字转字符
int num = 5;
char numC = (char) (num + '0');
//字符串转ASCII数组
String str = "Test";
int [] array = new int[str.length()];
for (int i = 0; i < str.length(); i++) {
array[i] = str.charAt(i);
}
// ASCII数组转字符串
byte [] bytes = {72,101,108,108,111};
System.out.println("ASCII数组转字符串 " + new String(bytes, StandardCharsets.US_ASCII));
String asciiStr = "hello";
List<Integer> asciiValList = new ArrayList<>();
for (int i = 0; i < asciiStr.length(); i++) {
int asciiVal = asciiStr.charAt(i);
asciiValList.add(asciiVal);
}
}
/**
* 输出26个字母
*/
public static void export26ZiMu() {
List<String> list = new ArrayList<String>();
for(char c = 'A'; c <= 'Z'; c++) {
list.add(c + "");
}
}
/**
* 数组排序
* @param prices
*/
public void arraySort(int [] prices) {
Arrays.sort(prices);
Integer [] integerAttr = new Integer[prices.length];
for (int i = 0; i < prices.length; i++) {
integerAttr[i] = prices[i];
}
Arrays.sort(integerAttr, Collections.reverseOrder());
Collections.sort(new ArrayList<Integer>(), Collections.reverseOrder());
//list数组排序
List<int []> list = new ArrayList<>();
//1. int数组中是一个值
Collections.sort(list, Collections.reverseOrder());
//2. int数组中是两个值
Collections.sort(list, (a, b) -> {
if (a[0] != b[0]) {
return a[0] - b[0];
} else {
return a[1] - b[1];
}
});
}
java基础转换
于 2025-10-26 17:27:21 首次发布

99

被折叠的 条评论
为什么被折叠?



