1

I have 3 arrays. I want to merge the 3 arrays and create a new array that shows all the merged integers. I would like this new array to be in round robin style.

Example input:

array = {arr1 = 1, 2, 3, arr2 = 4, 5, 6, arr3 = 7, 8, 9}

Example output:

arr4 = 1, 4, 7, 2, 5, 8, 3, 6, 9

I'm supposed to use this function but I don't understand how can I use a listOfLists:

String roundRobin(List<List<Integer>>listOfLists) {

4 Answers4

1

You could do it more simply using ArrayLists or Arrays.

With Arrays, you create 3 int[] and 1 int[][]. What this translates to is "3 arrays of ints and 1 array of arrays of ints". That is what #4 is: an array of your other arrays. In code it is:

int[]
    arr1 = {1, 2, 3},
    arr2 = {4, 5, 6},
    arr3 = {7, 8, 9};
int[][] arr4 = {arr1, arr2, arr3};

Alternatively, you could use ArrayLists, which differ from Arrays in that you can add or remove elements, among many other actions. The logic is the same in this case, just the syntax is different. You create 3 ArrayList<Integer> and 1 ArrayList<ArrayList<Integer>>, which translates to "3 ArrayLists of Integers and 1 ArrayList of ArrayLists of Integers." In code it is:

ArrayList<Integer>
    list1 = new ArrayList<>(Arrays.asList(1, 2, 3)),
    list2 = new ArrayList<>(Arrays.asList(4, 5, 6)),
    list3 = new ArrayList<>(Arrays.asList(7, 8, 9));

List<ArrayList<Integer>> list4 = new ArrayList<>
        (Arrays.asList(list1, list2, list3));

Finally, you can print the output of both methods:

System.out.println("Arrays - int[][]: " + Arrays.deepToString(arr4)
        + "\nLists - List<ArrayList<Integer>>: " + list4);

And you will get:

Arrays - int[][]: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Lists - List<List<Integer>>: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

Does this help?

greybeard
  • 2,249
  • 8
  • 30
  • 66
Cole Henrich
  • 155
  • 3
  • 17
1

You can use two nested for loops:

List<List<Integer>> lists = List.of(
        List.of(1, 2),
        List.of(3, 4, 5, 6),
        List.of(7, 8, 9));

List<Integer> listRobin = new ArrayList<>();

// infinite loop through the columns
for (int i = 0; ; i++) {
    // if the maximum number
    // of columns is reached
    boolean max = true;
    // loop through the rows, aka inner lists
    for (List<Integer> row : lists) {
        // if this column is present
        if (i < row.size()) {
            // column is present
            max = false;
            // take value from the column
            listRobin.add(row.get(i));
        }
    }
    // while the columns are still present
    if (max) break;
}

// output
System.out.println(listRobin);
// [1, 3, 7, 2, 4, 8, 5, 9, 6]

See also: Filling a jagged 2d array first by columns

0

I think just loop input and get element and add element to a list of array is easy to understand.

public static List<Integer> roundRobin(List<List<Integer>> listOfLists) {
    if (listOfLists == null || listOfLists.isEmpty()) {
        return new ArrayList<>();
    }

    int maxLength = -1;
    for (List<Integer> list : listOfLists) {
        if (list.size() > maxLength) {
            maxLength = list.size();
        }
    }

    List<Integer> result = new ArrayList<>(maxLength * listOfLists.size());
    for (int i = 0; i < maxLength; i++) {
        for (List<Integer> list : listOfLists) {
            if (i < list.size()) {
                result.add(list.get(i));
            }
        }
    }

    return result;

}
TongChen
  • 1,414
  • 1
  • 11
  • 21
0

To populate a 1d list from the columns of a 2d list, you can use two nested streams: first by columns, and then by rows. The length of the internal lists does not matter, in an outer stream you can traverse while the columns are still present.

List<List<Integer>> listOfLists = List.of(
        List.of(1, 2, 3, 4),
        List.of(5, 6),
        List.of(7, 8, 9));

List<Integer> listRobin = IntStream
        // infinite Stream through
        // the columns of a 2d list
        .iterate(0, i -> i + 1)
        // take the values from the column
        // Stream<List<Integer>>
        .mapToObj(i -> listOfLists
                // iterate over the inner lists
                .stream()
                // take those lists where
                // this column is present
                .filter(list -> list.size() > i)
                // take value from the column
                .map(list -> list.get(i))
                // return a new list
                .collect(Collectors.toList()))
        // while the columns are still present
        .takeWhile(list -> list.size() > 0)
        // flatten to a single stream
        // Stream<Integer>
        .flatMap(List::stream)
        // return a new list
        .collect(Collectors.toList());

// output
System.out.print(listRobin); // [1, 5, 7, 2, 6, 8, 3, 9, 4]

See also: Efficient way to choose data from several Lists with round robin algorithm