The shortest way to populate an ArrayList

What is the shortest way to populate an ArrayList? Sort of:

ArrayList<Integer[]> list = new ArrayList<Integer[]>(); list.add({1,10,1,1}); list.add({2,11,1,1}); 

Or:

 ArrayList<Integer[]> list = ({1,10,1,1},{2,11,1,1}); 
+6
source share
4 answers

To fix your first attempt:

 ArrayList<Integer[]> list = new ArrayList<Integer[]>(); list.add(new Integer[]{1,10,1,1}); list.add(new Integer[]{2,11,1,1}); 
+2
source

How about this shortcut:

 List<int[]> list = Arrays.asList( new int[][]{{1,10,1,1}, {2,11,1,1}} ); 
+12
source

Using guava

 Lists.newArrayList(new int[]{1,2,3}, new int[]{2,4,5}, new int[]{5,6,7}); 
+6
source
 List<Integer[]> list = new ArrayList<Integer[]>(); list.add(new Integer[] { 1, 10, 1, 1 }); list.add(new Integer[] { 2, 11, 1, 1 }); 

Or here is a single line:

 List<Integer[]> list = Arrays.asList(new Integer[] { 1, 10, 1, 1 }, new Integer[] { 2, 11, 1, 1 }); 
+1
source

Source: https://habr.com/ru/post/904990/


All Articles