2

I have two List<Int> of int like {1,2,3} and {4,5,6}.

I need to obtain a List<List<Int>> like: ((1,4),(2,5),(3,6))

How should i proceed? I tried with for but i can get only the cartesian product

pierbin24
  • 31
  • 5

3 Answers3

1

The key number here is 3: You want to loop 3 times. Each loop, you know what to do: Fetch the Xth number from input1, the Xth number from input2, put them together in a size-2 list, and that then forms the Xth entry in your output.

int[] in1 = ...;
int[] in2 = ...;
int[][] out;

assert input1.length == input2.length;
int len = input1.length;
out = new int[len][];
for (int i = 0; i < len; i++) {
  out[i] = new int[] {input1[i], input2[i]};
}
rzwitserloot
  • 85,357
  • 5
  • 51
  • 72
  • Thank you. My problem is that i have to return a List of List and i don't know how to manipulate it – pierbin24 Sep 08 '21 at 09:21
  • 1
    There is almost no difference between `int[][]` and `List>` - if I fix that, I'm just giving you stuff to copy and paste and you'd learn nothing. You should be able to trivially take the above and figure it out. – rzwitserloot Sep 08 '21 at 12:26
  • yes i know but i wasnt able to manipulate the index of the list as the index of the array, i post a possibile solution that work and i think is correct – pierbin24 Sep 08 '21 at 12:35
1
List<Integer> list1 = List.of(1,2,3);
List<Integer> list2 = List.of(4,5,6);
List<List<Integer>> merged = mergeLists(list1, list2);
System.out.println(merged); 

Prints

[[1, 4], [2, 5], [3, 6]]

This method accepts lists of any type <T> as long as they contain the same types and are the same length.

  • throw an exception if different lengths.
  • allocate a return ArrayList
  • initialize an index to 0
  • Using an enhanced for loop, iterate over one list and index the other
  • add a new list via List.of() to the return list. Since List.of() is immutable it is passed as an argument to new ArrayList<>()
  • return the result wnen the loop is finished.
public static <T> List<List<T>> mergeLists(List<T> list1,
        List<T> list2) {
    if (list1.size() != list2.size()) {
        throw new IllegalArgumentException("Lists must be the same size");
    }
    List<List<T>> list = new ArrayList<>();
    int i = 0;
    for (T v1 : list1) {
        list.add(new ArrayList<>(List.of(v1, list2.get(i++))));
    }
    return list;
}
WJS
  • 36,363
  • 4
  • 24
  • 39
0

Ok I solved the problem like this:

public List<List<V>> Lister (List<V> list1, List<V> list2){
    List<List<V>> result = new ArrayList<>();
    if (list1.size() == list2.size()) {
      for(int i =0; i<list1.size(); i++){
        List<V> tempList = new ArrayList<>();
        tempList.add(list1.get(i));
        tempList.add(list2.get(i));
        result.add(tempList);
      }
    }
    return result;
  }
pierbin24
  • 31
  • 5
  • I didn't get your problem, you have done it and assuming lists have the same size you could easily access it with another for cycle. https://pastebin.com/WbHR62pD – Virgula Sep 08 '21 at 10:13
  • Please add further details to expand on your answer, such as working code or documentation citations. – Community Sep 08 '21 at 11:20