simkusr
simkusr

Reputation: 812

Java how to append two or more arrays in to new array like in Python

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!");
        }

Upvotes: 0

Views: 603

Answers (2)

AxelH
AxelH

Reputation: 14572

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]]

Upvotes: 2

Saif Ahmad
Saif Ahmad

Reputation: 1171

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]] 

Upvotes: 1

Related Questions