1
把int转化为char数组(T738) char[] chars = String.valueOf(n).toCharArray();
int范围 在一般的电脑中,int占用4字节,32比特,数据范围为-2147483648~~2147483647[-2^31 2^31-1]。共十位数字,2后面9个0,10的9次方可以用int表示
* 把十进制的i换成n进制
private String bit(int i) {
StringBuilder sb = new StringBuilder();
while (i != 0){
sb.insert(0,i % n);
i /= n;
}
return sb.toString();
}
十进制和十六进制之间的转化
十进制转十六进制
String.format(“%08x”, 645741568);
十六进制转十进制
Long.parseLong(“267d3c00”, 16);
public static void main(String[] args) {
//十进制转十六进制
//转换后字母要求大写就是大写的X,转换后字母要求小写就是小写的x
System.out.print("645741568转十六进制:" + String.format("%08x", 645741568) + "\n");
//十六进制转十进制
System.out.print("267d3c00转十进制:" + Long.parseLong("267d3c00", 16));
}
把进制改成10进制,比如2进制的1111
public static void main(String[] args) {
System.out.println(Integer.parseInt("1111",2));//15
String s = "1011";
int k = 2;
System.out.println(Long.parseLong(s,k));//11
}
数字和字符之间的转化
public static void main(String[] args) {
int i = Integer.parseInt("123");
char[] arr = {'2','4'};
int j = arr[1] - '0';
String s = 34 + " ";
int sum = i + j;
System.out.println(sum);//127
System.out.println(s.charAt(1));//4
}
子串是连续的,子序列是不连续的,123456 的子序列可以是12346
组合和排列的区别:组合不讲究先后顺序,1,2的组合只有1,2 但是排列有12和21
排序:
排序后people=[[7,0],[7,1],[6,1],[5,0],[5,2],[4,4]]
第一个数字降序,第二个数字升序。
上面这个函数没有返回值,最后返回people就可以了
按第二位的升序排列:
可以改成lanmda表达式:
还可以定义规则:
(剑指offer 45)
遍历map:
只遍历map的key:
只遍历map的value:
new String() 和toString的坑:
getOrDefault函数的用法
Character类的常用静态字符判断方法
isAlphabetic() //判断传入的相应字符是否为字母(文字)(含中文文字)
isLetterOrDigit() //判断传入的相应字符是否为字母或数字(含中文文字)
isLetter() //判断传入的相应字符是否为字母或文字(含中文文字)
isUpperCase() //判断传入的相应字符是否为大写字母(仅限英文字母)
isisLowerCase() //判断传入的相应字符是否为小写字母(仅限英文字母)
isSpaceChar() //判断传入的相应字符是否为空格
关于集合的比较
public static void main(String[] args) {
Set<Integer> set = new HashSet<>();
Set<Integer> set1 = new HashSet<>();
set.add(1);set.add(2);
set1.add(2);set1.add(1);
System.out.println(set.equals(set1));//true
List<Integer> list = new ArrayList<>();
List<Integer> list1 = new ArrayList<>();
list.add(1); list.add(2);
list1.add(1);list1.add(2);
System.out.println(list.equals(list1));//true
Map<Integer,Integer> map = new HashMap<>();
Map<Integer,Integer> map1 = new HashMap<>();
map.put(2,3);map.put(3,4);
map1.put(2,3);map1.put(3,4);
System.out.println(map.equals(map1));//true
int[] arr = {1,2,3};
int[] arr1 = {1,2,3};
System.out.println(Arrays.equals(arr,arr1));//true
Deque<Integer> deque = new ArrayDeque<>();
Deque<Integer> deque1 = new ArrayDeque<>();
deque.addLast(3);deque.addLast(8);
deque1.addLast(3);deque1.addLast(8);
//deque 没法比较,只能转化为数组,然后用数组比较
System.out.println(Arrays.equals(deque.toArray(),deque1.toArray()));//true
}
list怎么删除元素:
list和set之间的转化
PriorityQueue(原理是堆排序,默认升序)
常用方法:remove,add,isEmpty,peek(peek是最小值)
自定义排序
class CreatTree {
public static class Student {
private int id;
private String name;
private int age;
public Student(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public int getId() {
return id;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
public static void main(String[] args) {
PriorityQueue<Student> queue = new PriorityQueue<>(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o2.getId()-o1.getId();
}
});
queue.add(new Student(2,"王昭君",18));
queue.add(new Student(1,"吕布",19));
queue.add(new Student(5,"貂蝉",17));
queue.add(new Student(3,"赵云",20));
queue.add(new Student(4,"荆轲",18));
System.out.println(queue.remove());
System.out.println(queue.remove());
System.out.println(queue.remove());
System.out.println(queue.remove());
System.out.println(queue.remove());
}
}
看看两个回溯总结
List
import java.util.*;
public class CreatTree {
public static void main(String[] args) {
Map<String, String> map1 = new HashMap<String, String>();
map1.put("aaa", "333");
map1.put("ads", "wewew");
map1.put("bb", "sdsdsdddds");
Map<String, String> map2 = new HashMap<String, String>();
map2.put("aaa", "222");
map2.put("ad", "sdssdsd");
map2.put("bb", "sdadas");
Map<String, String> map3 = new HashMap<String, String>();
map3.put("aaa", "111");
map3.put("ad", "sssdsds");
map3.put("bbs", "sdasssdas");
List<Map<String, String>> result = new ArrayList<>();
result.add(map1);
result.add(map2);
result.add(map3);
Collections.sort(result, new Comparator<Map<String, String>>() {
@Override
public int compare(Map<String, String> o1, Map<String, String> o2) {
// return o1.get("aaa").compareTo(o2.get("aaa"));//升序
return o2.get("aaa").compareTo(o1.get("aaa"));// 降序排序
}
});
System.out.println("\n hashMap排序方式, 根据value排序: \n" + result);
}
}