Skip to content
Advertisement

Create a list of List from an Array

How can i create a list of List from Array eg: int[] arr = {3, 1, 5, 8, 2, 4}. Such that the lists in the List have only two elements eg: [[3,1], [5,8], [2,4]].

So far i have tried code below but it return only lists with one element,I can’t figure out where i went wrong.

JavaScript

Result: [[3], [1], [5], [8], [2], [4]].

Advertisement

Answer

Here’s a generic one:

JavaScript

Output:

JavaScript

You are basically creating a mapping of index->value and subsequently grouping by the batchSize to make splits

Advertisement