cpl77
cpl77

Reputation: 23

Zipping two arrays in java

I would like to join two arrays but not like the usual way

{1,2,3} {4,5,6} 
{1,2,3,4,5,6}

I would like to join them like so

{1,2,3} {4,5,6} 
{1,4,2,5,3,6}

any suggestions?

I do not want to concatenate the two arrays but rather zip them

Upvotes: 2

Views: 1959

Answers (4)

skY
skY

Reputation: 111

O(m) + O(n) is the maximum you can achieve here and accurate answered have already been provided here. However if array size is extremity higher let's say 10^7 and if you'd want to reduce computation time little more by imposing on your machine cores and comfortable with slight complication in the code you can use concurrency here.

int[] arr1 = new int[10000010];
int[] arr2 = new int[10000000];
int end, endFinal;
int[] output = new int[arr1.length+arr2.length];

if( arr1.length < arr2.length )
    end = arr1.length;
 else
    end = arr2.length;

endFinal = arr1.length + arr2.length;

 T obj1 = new T( arr1, output, 0, end );
 T obj2 = new T( arr2, output, 1, end );

  Thread t1 = new Thread(obj1);
  Thread t2 = new Thread(obj2);
  t1.start();
  t2.start();
  t1.join();
  t2.join();

  for( int j = 2*end; j<endFinal; j++)
      {
           if(endFinal == arr1.length)
               output[j] = arr1[end++];
           else
               output[j] = arr1[end++];
       }
  }
class T implements Runnable {
      int[] arr, output;
      int start, end;
      public T ( int[] arr, int[] output, int start, int end)
           {
                this.arr = arr;
                this.output = output;
                this.start = start;
                this.end = end;
       }

        public void run (){
              int count = 0;
              for (int i = start; i< end; i+=2)
                   output= arr[count++];
       }
 }

So both the threads will populate m number of elements assuming m is less than n and rest n - m forms will be populated normally. This may seem like a complicated solution and should only be used if necessary. One may not see the difference in smaller arrays.

Upvotes: 0

Kamil Tomasz Jarmusik
Kamil Tomasz Jarmusik

Reputation: 139

VerA - on List and Integer, returned Integer[]; VerB - on array and int, returned int[]; Mixes n arrays where the length can be different.

public static Integer[] arraysMixVerA(Integer[]... arrays) {
    List<Integer> list = new ArrayList<>();
    int max=-1;
    for (Integer[] array : arrays) {
        max = Math.max(max, array.length);
    }
    for (int i = 0; i < max*arrays.length; i++) {
        list.add(null);
    }
    for (int i = 0; i < arrays.length; i++) {
        for (int j = 0; j < arrays[i].length; j++) {
            list.set(j * arrays.length + i, arrays[i][j]);
        }
    }
    for(int i=0;i<list.size();i++){
        if(list.get(i)==null)
            list.remove(i);
    }
    return list.toArray(new Integer[list.size()]);
}

public static int[] arraysMixVerB(int[]... arrays) {

    int max=-1;
    int sumaIndex=0;
    for (int[] array : arrays) {
        max = Math.max(max, array.length);
        sumaIndex=sumaIndex+array.length;
    }
    Integer[] temp=new Integer[max*arrays.length]; //For an array of 
      //type int, the default value is 0. For an array of type Integer, 
     //the default value is null. Thus could not be distinguish zeros with arrays
    //that are arguments methods of zeros from the array "temp".
    int[] target=new int[sumaIndex];

    for (int i = 0; i < arrays.length; i++) {
        for (int j = 0; j < arrays[i].length; j++) {
            temp[j * arrays.length + i]=arrays[i][j];
        }
    }
    for(int i=0,j=0;i<temp.length;i++){
        if(temp[i]!=null){
            target[j++]=temp[i];
        }

    }
    return target;
}

Upvotes: 0

sprinter
sprinter

Reputation: 27976

Here is a technique using Java 8 streams:

int[] mergedArray = IntStream.range(0, Math.min(array1.length, array2.length))
    .flatMap(n -> IntStream.of(array1[n], array2[n]))
    .toArray();

Upvotes: 3

Vikrant Kashyap
Vikrant Kashyap

Reputation: 6856

This Program also work for if array size of both is not equals.. Happy to help

  public class Assignment{

    public static void main(String[] args){
    int [] arr1 = new int[]{1,2,3};
    int [] arr2 = new int[]{4,5,6,7,8};
    int [] arr3 = new int[arr1.length + arr2.length];
    int biglength = 0;
     if(arr1.length > arr2.length){
         biglength = arr1.length;
     }else{
         biglength = arr2.length;
     }
    for(int i=0,j=0; i< biglength; i++){
         if(i<arr1.length && i<arr2.length){
            arr3[j++] = arr1[i];
            arr3[j++] = arr2[i];
         }else if(i<arr1.length){
            arr3[j++] = arr1[i];
         }else{
            arr3[j++] = arr2[i];
         }
    }
   for(int j= 0 ; j<arr3.length; j++){
        System.out.print(arr3[j]);
     }
  }
    }

Upvotes: 3

Related Questions