How to make a flat list out of a list of lists?
I want to flatten this list of lists: [[1, 2, 3], [4, 5, 6], [7], [8, 9]] into: [1, 2, 3, 4, 5, 6, 7, 8, 9] 2 28
I want to flatten this list of lists: [[1, 2, 3], [4, 5, 6], [7], [8, 9]] into: [1, 2, 3, 4, 5, 6, 7, 8, 9] 2 28
[…] How should Java Comparator class be declared to sort the arrays by their first elements in decreasing order […] Here’s a complete example using Java 8: import java.util.*; public class Test { public static void main(String args[]) { int[][] twoDim = { {1, 2}, {3, 7}, {8, 9}, {4, 2}, {5, 3} }; Arrays.sort(twoDim, … Read more
Consider public static void main(String[] args) { int[][] foo = new int[][] { new int[] { 1, 2, 3 }, new int[] { 1, 2, 3, 4}, }; System.out.println(foo.length); //2 System.out.println(foo[0].length); //3 System.out.println(foo[1].length); //4 } Column lengths differ per row. If you’re backing some data by a fixed size 2D array, then provide getters to … Read more
Looks like this is what you want int columns = 2; int rows = 2; String[][] newArray = new String[columns][rows]; newArray[0][0] = “France”; newArray[0][1] = “Blue”; newArray[1][0] = “Ireland”; newArray[1][1] = “Green”; for(int i = 0; i < rows; i++){ for(int j = 0; j < columns; j++){ System.out.println(newArray[i][j]); } } Here I’ll explain the … Read more