Java各种初始化
-
int[] res = new int[]{-1, -1};
-
String res = “”;
-
输出类型List<List>如何被初始化?List<List> res = new ArrayList<>(); 有关这些复杂类型的初始化还有待于进一步研究
-
List res = new ArrayList<>();//不确定长度的list
-
在Java中 是不是List用[] 但是Array用{}
-
二维数组:
boolean[][] dp = new boolean[s.length()][s.length()];//二维数组也是array,因此初始化的时候要么长度给定 要么直接初始化元素,见下: -
二维数组:
int[][] a = {{1,2}{3,4}} -
HashMap<Integer, Integer> map = new HashMap<>();
-
值得注意的是 hashmap不能直接用值进行初始化(?)只能用map.put(k,v)进行放入值
-
HashSet set = new HashSet<>();
-
array的几种初始化方式:
int[] a; int b[]; //数组的声明 int[] array1 = {4,5,6}; int[] array2 = new int[3]; int[] array3 = new int[]{1,2,3}//数组的初始化 -
输出链表初始化:
ListNode dummy = new ListNode(0);而且要注意的是 链表的使用一般都是要链表指针的 如下:ListNode cur = dummy;//链表的遍历是通过指针来完成 -
StringBuilder类型数据初始化:
StringBuilder res = new StringBuilder(); -
数组型StringBuilder类型数据初始化:
StringBuilder[] res = new StringBuilder[number_of_rows];//与数组结合 -
Stack初始化
Stack<Character> stack = new Stack<Character>(); -
PQ初始化
PriorityQueue<ListNode> queue = new PriorityQueue<>(lists.length, (a, b) -> a.val - b.val);或者Queue<TreeNode> queue = new LinkedList<>();OR:PriorityQueue<Integer> heap = new PriorityQueue<Integer>((n1, n2) -> map.get(n1) - map.get(n2));//and heap will be map with sorted in its value -
Deque的初始化:
Deque<Integer> deque = new LinkedList<>(); -
二维数据的初始化总结如下:(理论上来说 能用Array的都可以用ArrayList去做 只是有时候输出的要求已经定好 最后必须进行相关转化)
-
内层外层都需要ArrayList(即内外层长度都不定): 这种情况很常见,直接
List<List<Integer>> res = new ArrayList<>(); List<Integer> list = new ArrayList<>(); -
内层外层都需要Array(内层外层长度都固定):
int[][] res = new int[length1][length2]; int[] list = new int[length]; -
外层ArrayList,内层Array(外层长度不固定 内层长度固定 比如LC56):
List<int[]> list = new ArrayList<>(); -
外层Array,内层ArrayList(外层长度固定 内层长度不固定,比如LC347 Top K frequency elements):
List<Integer>[] array = new List[length];//it’s like int[] but instead of int,we have List -
此外 如何把这些二维数据的格式之间相互转化?Arrays.asList(), var.toArray()看起来没什么用处
-
random object: ‘Random rand = new Random();’
-
sort String array based on it length(small->larger):
-
Arrays.sort(words, new Comparator<String>() {
@Override
public int compare(String s1, String s2) {return s1.length() - s2.length();}
}); -
sort 2d array based on the first column:
-
Arrays.sort(words, new Comparator<int[]>() {
@Override
public int compare(int[] interval1, int[] interval2) {return interval[0] - interval[1];}
}); //not sure if this gonna work -
another way:
-
Arrays.sort(intervals, (a, b) -> (a[0]- b[0]));
-
Min heap and max Heap:
PriorityQueue<Integer> minheap = new PriorityQueue<>()PriorityQueue<Integer> maxheap = new PriorityQueue<>(Comparator.reverseOrder());or maxheap can be initialize as : -
PriorityQueue<Integer> maxHeap = new PriorityQueue<Integer>( new Comparator<Integer>() { public int compare(Integer i1, Integer i2) { return i2.compareTo(i1); } } ); -
convert hashmap key set to list:
List<String> res_files = new ArrayList<>(t.files.keySet()) -
we want to sort a list of Node, and nodes have attribite of times and sentence. we want all those node sort in times first, if they are equal, then we sort them in sentence:
Collections.sort(list, (a, b) -> a.times == b.times? a.sentence.compareTo(b.sentence): b.times - a.times); -
initialize a hashset with values:
List<Character> list = Arrays.asList('a','e','i','o','u'); HashSet<Character> set = new HashSet<>(list);or you can use Collections.addAll(), or you can initialize a set and add items one by one. -
String to character array: s.toCharArray(), charArray to String: new String(char[] chars)
-
how to traverse hashset? just use for each, or use iterator.

浙公网安备 33010602011771号