2

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

4
  • I believe that's called zipping the two arrays. Commented Feb 26, 2016 at 4:26
  • 2
    Here's one approach: stackoverflow.com/questions/2682053/… Commented Feb 26, 2016 at 4:27
  • 1
    Possible duplicate of How can I concatenate two arrays in Java? Commented Feb 26, 2016 at 4:28
  • @Thilo thank you for naming it, i was trying to find it but all i found was for concatenation. Commented Feb 26, 2016 at 5:23

4 Answers 4

3

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]); } } } 
Sign up to request clarification or add additional context in comments.

Comments

3

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(); 

Comments

0

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; } 

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.