0

I wrote this question because I can't seem to find any answer that suits my needs.

What I'm trying to achieve: I want to create new array e.g. array3 in java that would hold two or more arrays from different arrays, e.g. array3 = [[array1], [array2], [arrayN]].

In Python I know how to append 2 lists to 3rd list, like:

list1 = [1, 2, 3]
list2 = [11, 22, 33]
list3 = []

for i in range(len(list1)):
    list3.append([list1[i], list2[i]])

print(list3)

and result will be: [[1, 11], [2, 22], [3, 33]]

And I can't find the right answer how to do this in Java. Is there any way to achieve this goal in Java?

Added what I have done yeat:

        String[] list1 = integer1.split(";");
        String[] list2 = integer2.split(";");
        String[] list3 = integer3.split(";");
        String[] list4 = integer4.split(";");

        int lenOfList1 = list1.length;
        int lenOfList2 = list2.length;
        int lenOfList3 = list3.length;
        int lenOfList4 = list4.length;

        int result1 = Integer.compare(lenOfList1, lenOfList2);
        int result2 = Integer.compare(lenOfList3, lenOfList4);
        int result3 = Integer.compare(result1, result2);

        ArrayList<String> ar = new ArrayList<String>();

        if (result3 == 0) {
            System.out.println("This is result: " + result3 + ", and we are good to go now!");
            for(int i=0; i < lenOfList1; i++){
                ar.add(list1[i]);
                ar.add(list2[i]);
                ar.add(list3[i]);
                ar.add(list4[i]);
            }
        } else {
            System.out.println("This is result: " + result3 + ", and this is the end!");
        }
9
  • 1
    Do you have any Java code to show? Have you made a start? Commented Feb 21, 2018 at 9:10
  • I have achieved only this [1,2,3,4] from different arrays. Commented Feb 21, 2018 at 9:11
  • Maybe take a look at this. Commented Feb 21, 2018 at 9:12
  • @rolandas simkus you mean array[array]?? Commented Feb 21, 2018 at 9:12
  • 1
    @RolandasŠimkus both arrays will contain same size all the time or there might be possibility to mismatch in them? Commented Feb 21, 2018 at 9:25

2 Answers 2

2

I would simply create a method to regroup lists in a 2 dimension one.

Using a simple varargs method like :

 public static <T> List<List<T>> groupArrays(T[]... arrays){

That way, you can pass as many arrays you want, to that method. The implementation would look like

public static <T> List<List<T>> groupArrays(T[]... arrays){
    if(arrays.length == 0){
        throw new IllegalArgumentException("No arrays to concat");
    }

    //Find the longuest array to know how many inner list to create
    int maxLength = arrays[0].length;
    for(int i = 1; i < arrays.length; ++i){
        maxLength = Math.max(maxLength, arrays[1].length);
    }

    //creating those now reduce the complexity in the next loop.
    List<List<T>> lists = new ArrayList<>();
    for(int i = 0; i < maxLength; ++i){
        lists.add(new ArrayList<>());
    }

    for(T[] array : arrays){
        for(int i = 0; i < array.length; ++i){
            lists.get(i).add(array[i]);
        }
    }

    return lists;
}

Then, just call it like :

Integer[] a1 = {1, 2};
Integer[] a2 = {11, 22, 33, 44};
Integer[] a3 = {111, 222, 333};

List<List<Integer>> result = groupArrays(a1, a2, a3);
System.out.println(result);

[[1, 11, 111], [2, 22, 222], [33, 333], [44]]

Sign up to request clarification or add additional context in comments.

Comments

1

You can try using List<List<dataType>>

For you:

List<Integer> list1 = new ArrayList<>();
list1.add(1);
list1.add(2);
list1.add(3);

List<Integer> list2 = new ArrayList<>();
list2.add(11);
list2.add(22);
list2.add(33);

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

for (int i = 0; i <list1.size(); i++) {

    List<Integer> tempList = new ArrayList<>();
    tempList.add(list1.get(i));
    tempList.add(list2.get(i));
    list3.add(tempList);
}
System.out.println(list3);

OUTPUT:

[[1, 11], [2, 22], [3, 33]] 

1 Comment

Thank you, this is what I was looking for. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.