美文网首页
2020-12-24

2020-12-24

作者: 淡水河谷123 | 来源:发表于2020-12-24 23:58 被阅读0次

1.Arrays类
列表初始化
List<String> stringList = Arrays.asList("a", "b", "c");
排序
Arrays.sort(array);
Arrays.sort(array, Collections.reverseOrder());

匿名对象实现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表达式方式
Arrays.sort(arr, (a,b) -> a[0]==b[0] ? a[1]-b[1] : a[0]-b[0]);

数组复制
int[] a2 = a1.clone();

数组转字符串
Arrays.toString(array)

数组截取
System.arraycopy(arr2,0,arr1,0,3);
将arr1从0开始长度为3的元素赋值到arr2中长度从0开始的元素

2.队列的实现
Queue<String> queue = new LinkedList<String>();
queue.offer("a")
queue.peek()
queue.poll()
3.栈的实现
Stack<Integer> st = new Stack<Integer>();
st.push(1);
st.peek( )
st.pop();
st.empty();

4.创建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.优先队列
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);

相关文章

网友评论

      本文标题:2020-12-24

      本文链接:https://www.haomeiwen.com/subject/ksovnktx.html