Java 常用操作的总结,主要用作个人备忘录,以后视情况会更新。
Basic
三目运算符
If boolean expression is true, then return value of a; Otherwise return value of b.
1 2 3 4 5 6 7 8 9 10
| s = Boolean Expression ? exp. a : exp. b;
cur.next = (l1 == null) ? l2 : l1;
if (l1 == null) cur.next = l2; else cur.next = l1;
|
输入操作
1 2 3 4 5
| Scanner in = new Scanner(System.in);
String a = in.nextLine();
int b = in.nextInt();
|
String 操作
String 初始化:(两种)
1 2 3
| String a = "hello";
String b = new String("world");
|
String 相关方法
1 2 3 4 5 6 7 8 9 10
| a.length();
a.equalsIgnoreCase(b);
a == b;
for (int i = 0; i < s.length(); ++i) char c = s.charAt(i);
|
String连接
1 2 3 4 5
| String a = "hello"; String b = "world";
String s1 = a + b; String s2 = a.concat(b);
|
常用提取方法
1 2 3 4 5 6 7 8 9 10 11 12
| s.charAt(i);
s.indexOf(char c); s.lastIndexOf();
s.indexOf(String s);
s.substring(int i);
s.substring(int i, int j);
s.trim();
|
StringBuilder
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| StringBuilder s = new StringBuilder("hello");
s.toString();
s.reverse();
s.append();
s.replace(i, j, str); s.setChatAt(index, char);
|
Array 操作
数组基本操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| String [] name = new String[5];
int [] age = new int[3]; int [] age = {16, 17, 18};
age[2] = 19;
arr.add(val);
System.out.println( Arrays.toString(a) );
|
数组遍历
1 2 3 4 5
| for (int n : age) { }
for (char c : s.toCharArray())
|
二维数组
1
| int [][] matrix = new int[3][4];
|
常用操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| import java.util.Arrays;
int [] a = new int[10];
Arrays.fill(a, 1);
int n = Arrays.length;
Arrays.sort(a);
Arrays.binarySearch(a, 20);
Arrays.equals(a, b);
Arrays.asList(arr).indexOf(element);
Arrays.asList(Arrays.asList(1, 2), Arrays.asList(3, 4));
|
类型变换
Base conversion: https://stackoverflow.com/a/19607058
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| char c; int n = c - '0';
int n = Integer.parseInt("1001", 2);
int n = Integer.parseInt(s); int n = Integer.valueOf(s);
String s = Integer.toString(n);
int[] ans = Integer.toString(n).chars().map(c -> c-'0').toArray();
|
Sentence to Array of strings:
This can be accomplished just with split
as it takes regex:
1 2
| String s = "This is a sample sentence with []s."; String[] words = s.split("\\W+");
|
This will give words as: {"this","is","a","sample","sentence", "s"}
The \\W+
will match all non-alphabetic characters occurring one or more times. So there is no need to replace.
其他操作
设置临界值
1 2 3 4 5 6 7
| int a = Integer.MIN_VALUE; int b = Integer.MAX_VALUE;
long a = Long.MIN_VALUE; long b = Long.MAX_VALUE;
|
常用数学方法
1 2 3 4 5 6 7 8 9 10 11
| Math.abs(int);
Math.max();
Math.min();
Math.pow(2, 4);
|
Array List
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| ArrayList<Integer> a = new ArrayList<>();
a.add(1);
Object[] arr = a.toArray();
Collections.sort(a);
Collections.reverse(a);
a.add(0, element);
if(n == 0) return new ArrayList();
len = a.size();
a.get(index);
|
Linked List
1 2 3 4 5 6 7
| ListNode dummy = new ListNode(0); ListNode cur = dummy;
LinkedList<List<Integer>> l = new LinkedList<List<Integer>>();
|
Queue
1 2 3 4 5 6 7 8
| Queue<TreeNode> q = new LinkedList<>();
q.offer(root);
q.poll();
|
Deque
Deque = Double Ended Queue (can be accessed by both ends), organizes element in LIFO style
The documentation advise us to use the Deque
interface which offers a more consistent API
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| Deque<Integer> dq = new LinkedList<>();
dq.addFirst(1); dq.offerFirst(1);
dq.addLast(1); dq.offerLast(1); dq.offer(1);
dq.poll(); dq.pollFirst();
dq.pollLast();
|
HashMap
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| Map<Integer, Integer> d = new HashMap<>();
d.put(key, value);
d.get(key);
d.containsKey(key);
d.containsValue(value);
|
Stack
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| Stack<Character> s = new Stack<>();
s.add(c); s.push(c);
s.pop();
s.isEmpty();
s.size();
s.peek();
|