Java常用方法

String

字符串前面补0

1
2
String str = String.format("%08d", 123);
System.out.println(str); // 00000123

字符串后面补0

1
2
String str = String.format("%-8d", 123).replace(" ", "0");
System.out.println(str); // 12300000

保留指定位数小数

1
2
3
4
int x = 1328;
float num = 13.283457f;
System.out.printf("%.4f", num); // 13.2835
System.out.printf("%d", x); // 1328

格式化输出

1
2
3
4
int a = 1328;
float b = 13.283457f;
String str = String.format("%d %.4f", a, b);
System.out.println(str); // 1328 13.2835

字符串替换

1
2
3
4
5
String str="hellollo";
System.out.println(str.replace('h', 'k')); // kellollo
System.out.println(str.replace("ll", "ww")); // hewwowwo
System.out.println(str.replaceFirst("ll", "ww")); // hewwollo
System.out.println(str.replaceAll("ll", "ww")); // hewwowwo

字符串截取

1
2
3
4
String str ="Hello";
System.out.println(str.substring(1)); // ello
System.out.println(str.substring(2, 4)); // ll
System.out.println(str.subSequence(2, 4)); // ll

Integer

构造方法

1
2
3
public Integer(int value) {
this.value = value;
}
1
2
3
public Integer(String s) throws NumberFormatException {
this.value = parseInt(s, 10);
}
1
2
3
4
5
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}

字符串转整数

1
2
3
public static int parseInt(String s) throws NumberFormatException {
return parseInt(s,10);
}
1
2
3
public static int parseInt(String s, int radix) throws NumberFormatException {
// 将给定进制的字符串转换为10进制整数
}

整数转给定进制字符串

1
2
3
public static String toString(int i, int radix) {
// i表示要转换的整数,radix表示要转换的进制
}

BigInteger

构造方法

1
2
3
public BigInteger(String val) {
this(val, 10);
}
1
2
3
public BigInteger(String val, int radix) {
// 将给定进制的字符串转换为10进制大数
}
1
2
3
public static BigInteger valueOf(long val) {
// 传入一个long类型的值
}

大数转换给定进制字符串

1
2
3
public String toString(int radix) {
// radix代表要转换的进制
}

Arrays

数组排序

1
Arrays.sort();

数组转成list

1
2
String[] arr = {"a", "b", "c"};
List<String> list = Arrays.asList(arr);

Collections

集合排序

1
Collections.sort();

集合反转

1
Collections.reverse();

System

保留2位小数

1
System.out.printf("%.2f", 78.1276); // 78.13

List

list转成数组

1
2
3
4
5
6
List<String> list = new ArrayList<>();
list.add("a");
list.add("b");
list.add("c");
String[] arr = new String[list.size()];
list.toArray(arr);