天天看点

Java各种初始化

int[] res = new int[]{-1, -1};

String res = “”;

输出类型List<List>如何被初始化?List<List> res = new ArrayList<>(); 有关这些复杂类型的初始化还有待于进一步研究

List res = new ArrayList<>();//不确定长度的list

在Java中 是不是List用[] 但是Array用{}

二维数组: <code>boolean[][] dp = new boolean[s.length()][s.length()];</code>//二维数组也是array,因此初始化的时候要么长度给定 要么直接初始化元素,见下:

二维数组:<code>int[][] a = {{1,2}{3,4}}</code>

HashMap&lt;Integer, Integer&gt; map = new HashMap&lt;&gt;();

值得注意的是 hashmap不能直接用值进行初始化(?)只能用map.put(k,v)进行放入值

HashSet set = new HashSet&lt;&gt;();

array的几种初始化方式:<code>int[] a; int b[]; //数组的声明 int[] array1 = {4,5,6}; int[] array2 = new int[3]; int[] array3 = new int[]{1,2,3}//数组的初始化</code>

输出链表初始化:<code>ListNode dummy = new ListNode(0);</code> 而且要注意的是 链表的使用一般都是要链表指针的 如下:<code>ListNode cur = dummy;//链表的遍历是通过指针来完成</code>

StringBuilder类型数据初始化: <code>StringBuilder res = new StringBuilder();</code>

数组型StringBuilder类型数据初始化:<code>StringBuilder[] res = new StringBuilder[number_of_rows];//与数组结合</code>

Stack初始化<code>Stack&lt;Character&gt; stack = new Stack&lt;Character&gt;();</code>

PQ初始化<code>PriorityQueue&lt;ListNode&gt; queue = new PriorityQueue&lt;&gt;(lists.length, (a, b) -&gt; a.val - b.val);</code>或者<code>Queue&lt;TreeNode&gt; queue = new LinkedList&lt;&gt;();</code> OR: <code>PriorityQueue&lt;Integer&gt; heap = new PriorityQueue&lt;Integer&gt;((n1, n2) -&gt; map.get(n1) - map.get(n2));</code>//and heap will be map with sorted in its value

Deque的初始化:<code>Deque&lt;Integer&gt; deque = new LinkedList&lt;&gt;();</code>

二维数据的初始化总结如下:(理论上来说 能用Array的都可以用ArrayList去做 只是有时候输出的要求已经定好 最后必须进行相关转化)

内层外层都需要ArrayList(即内外层长度都不定): 这种情况很常见,直接<code>List&lt;List&lt;Integer&gt;&gt; res = new ArrayList&lt;&gt;(); List&lt;Integer&gt; list = new ArrayList&lt;&gt;();</code>

内层外层都需要Array(内层外层长度都固定): <code>int[][] res = new int[length1][length2]; int[] list = new int[length];</code>

外层ArrayList,内层Array(外层长度不固定 内层长度固定 比如LC56): <code>List&lt;int[]&gt; list = new ArrayList&lt;&gt;();</code>

外层Array,内层ArrayList(外层长度固定 内层长度不固定,比如LC347 Top K frequency elements): <code>List&lt;Integer&gt;[] array = new List[length];</code>//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-&gt;larger):

Arrays.sort(words, new Comparator&lt;String&gt;() {

@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&lt;int[]&gt;() {

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) -&gt; (a[0]- b[0]));

Min heap and max Heap:<code>PriorityQueue&lt;Integer&gt; minheap = new PriorityQueue&lt;&gt;()</code> <code>PriorityQueue&lt;Integer&gt; maxheap = new PriorityQueue&lt;&gt;(Comparator.reverseOrder());</code> or maxheap can be initialize as :

<code>PriorityQueue&lt;Integer&gt; maxHeap = new PriorityQueue&lt;Integer&gt;( new Comparator&lt;Integer&gt;() { public int compare(Integer i1, Integer i2) { return i2.compareTo(i1); } } );</code>

convert hashmap key set to list: <code>List&lt;String&gt; res_files = new ArrayList&lt;&gt;(t.files.keySet())</code>

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: <code>Collections.sort(list, (a, b) -&gt; a.times == b.times? a.sentence.compareTo(b.sentence): b.times - a.times);</code>

initialize a hashset with values: <code>List&lt;Character&gt; list = Arrays.asList('a','e','i','o','u'); HashSet&lt;Character&gt; set = new HashSet&lt;&gt;(list);</code> 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.