1.Arrays類
列表初始化
List<String> stringList = Arrays.asList("a", "b", "c");
排序
Arrays.sort(array);
Arrays.sort(array, Collections.reverseOrder());
匿名對(duì)象實(shí)現(xiàn)Comparator接口的方式
int[][] arr = new int[length][2];
Arrays.sort(arr, new Comparator<int[]>() {
public int compare(int[] o1, int[] o2) {
if(o1[0]==o2[0])
return o1[1]-o2[1];
return o1[0]-o2[0];
}
});
lambda表達(dá)式方式
Arrays.sort(arr, (a,b) -> a[0]==b[0] ? a[1]-b[1] : a[0]-b[0]);
數(shù)組復(fù)制
int[] a2 = a1.clone();
數(shù)組轉(zhuǎn)字符串
Arrays.toString(array)
數(shù)組截取
System.arraycopy(arr2,0,arr1,0,3);
將arr1從0開(kāi)始長(zhǎng)度為3的元素賦值到arr2中長(zhǎng)度從0開(kāi)始的元素
2.隊(duì)列的實(shí)現(xiàn)
Queue<String> queue = new LinkedList<String>();
queue.offer("a")
queue.peek()
queue.poll()
3.棧的實(shí)現(xiàn)
Stack<Integer> st = new Stack<Integer>();
st.push(1);
st.peek( )
st.pop();
st.empty();
4.創(chuàng)建hash表
List[] hashmap= new List[numCourses];
for(int i=0;i<numCourses;i++){
hashmap[i]=new ArrayList<Integer>();
}
5.String類
"abc".toCharArray()
"a b c".split(" ")
"abc ".substring(0,"abc ".length()-1)
6.Integer類
Integer.MIN_VALUE
Integer.MAX_VALUE
Integer.valueOf("123")
7.優(yōu)先隊(duì)列
Queue<Integer> integerPriorityQueue = new PriorityQueue<>(7);
queue.add(1);
queue.poll();
Queue<ListNode> integerPriorityQueue = new PriorityQueue<ListNode>(lists.length,(a,b) -> a.val-b.val);