java刷题笔记
二进制转换
将一个整数转化成二进制的方法:
1 方法1:使用BigInteger类:
@Test
public void test1(){
BigInteger b=new BigInteger("10");
System.out.println(b.toString(2));
b=new BigInteger("1");
System.out.println(b.toString(2));
b=new BigInteger("255");
System.out.println(b.toString(2));
b=new BigInteger("254");
System.out.println(b.toString(2));
}
2 方法2:使用Integer.toBinaryString():
@Test
public void test(){
String str2 = Integer.toBinaryString(0);
System.out.println(str2);
str2 = Integer.toBinaryString(1);
System.out.println(str2);
str2 = Integer.toBinaryString(255);
System.out.println(str2);
}
重写比较器
Arrays.sort(nums, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
return o2[0]-o1[0];
}
});