天天看點

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.