How to convert Array to List

Plain Java public void givenUsingCoreJava_whenArrayConvertedToList_thenCorrect() { Integer[] sourceArray = { 0, 1, 2, 3, 4, 5 }; List<Integer> targetList = Arrays.asList(sourceArray); } Commons Collection public void givenUsingCommonsCollections_whenArrayConvertedToList_thenCorrect() { Integer[] sourceArray = { 0, 1, 2, 3, 4, 5 }; List<Integer> targetList = new ArrayList<>(6); CollectionUtils.addAll(targetList, sourceArray); } Guava public void givenUsingGuava_whenListConvertedToArray_thenCorrect() { List<Integer> sourceList = …

How to sort 2D array in Java

My code : Arrays.sort(intervals, new Comparator<int[]>(){ @Override public int compare( int[] a, int[] b ){ return a[0]==b[0] ? a[1] – b[1] : a[0] – b[0]; // return Integer.compare( a[0], b[0] ); } }); And here is an simple way: Arrays.sort(myArr, (a, b) -> a[0] – b[0]);

Combination Sum

This is a problem in leetcode. To solve this problem, I decide to use recursion on it: class Solution { public List<List<Integer>> combinationSum(int[] candidates, int target) { List<List<Integer>> res = new ArrayList<List<Integer>>(); List<Integer> rec = new ArrayList<Integer>(); search( candidates, target, res, rec, 0); return res; } private void search(int[] arr, int tar, List<List<Integer>>res, List<Integer>rec, int …

Rotate Array

This is No.189 problem in LeetCode. The problem description is: Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: Input: [1,2,3,4,5,6,7] and k = 3 Output: [5,6,7,1,2,3,4] Explanation: rotate 1 steps to the right: [7,1,2,3,4,5,6] rotate 2 steps to the right: [6,7,1,2,3,4,5] rotate 3 steps to …