Concatenating two arrays with alternating values

↘锁芯ラ 提交于 2021-02-04 14:19:06

问题


What is the best way to concatenate two arrays with alternating values?

Let's say array1 is:

[1, 3, 5, 7]

array2 is:

[2, 4, 6, 8]

I want to combine these two arrays, so that the result is:

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

In Java:

int[] a1 = { 1, 3, 5, 7 };
int[] a2 = { 2, 4, 6, 8 };
int[] concat = new int[a1.length * 2];
for (int i = 0; i < concat.length; i++) {
    // concatenation
}
System.out.println(concat.toString());
// should be [1, 2, 3, 4, 5, 6, 7, 8]

Update: No sorting is required, as the arrays are already sorted, using Arrays.sort(array)


回答1:


A basic way

int[] concat = new int[a1.length * 2];
int index = 0;
for (int i = 0; i < a1.length; i++) {
    concat[index++] = a1[i];
    concat[index++] = a2[i];
}

assuming that both array will be of same size.




回答2:


Put the elements of both array in a list and then sort it.You can use lambdas also

Integer[] a1 = { 1, 3, 5, 7 };
Integer[] a2 = { 2, 4, 6, 8 };
List<Integer> list = new ArrayList<>();
list.addAll(Arrays.asList(a1));
list.addAll(Arrays.asList(a2));
System.out.println("Before Sorting "+list);
Collections.sort(list,(a, b) -> Integer.compare(a,b));
System.out.println("After Sorting "+list);

Output

Before Sorting [1, 3, 5, 7, 2, 4, 6, 8]
After Sorting [1, 2, 3, 4, 5, 6, 7, 8]



回答3:


If you want to zip together any length arrays (where then lengths differ, the remaining is appended to the result):

public static int[] zip(int[] a, int[] b){
    int[] result = new int[a.length + b.length];
    int index = 0;
    final int minLen = Math.min(a.length, b.length);
    for (int i = 0; i < minLen; i++) {
        result[index++] = a[i];
        result[index++] = b[i];
    }
    if(a.length > minLen)
      System.arraycopy(a, minLen, result, index, a.length - minLen);
    else if(b.length > minLen)
      System.arraycopy(b, minLen, result, index, b.length - minLen);
    return result;
}



回答4:


Try it like this:

int[] concat = new int[a1.length + a2.length];

int k = 0, m = 0;

for (int i = 0; i < concat.length; i++) {
    if( k < al.length && a1[k] <= a2[m])
        concat[i] = a1[k++];
    else
        concat[i] = a2[m++];
}

NB: The result will be sorted as in your desired output.




回答5:


you could also use two variables in your loop like this

int[] a1 = { 1, 3, 5, 7 };
int[] a2 = { 2, 4, 6, 8 };
int[] concat = new int[a1.length + a2.length];
for (int i = 0, j = 0; i+j < concat.length;) {
    if(i<a1.length) {
        concat[i+j] = a1[i++];
    }
    if(j<a2.length) {
        concat[i+j] = a2[j++];
    }

}
System.out.println(Arrays.toString(concat));



回答6:


Try This if it solves ur problem

int[] a1 = { 1, 3, 5, 7 };
int[] a2 = { 2, 4, 6, 8 };
int[] concat = new int[a1.length + a2.length];
System.arraycopy(a1, 0, concat, 0, a1.length);
System.arraycopy(a2, 0, concat, a1.length, a2.length);
Arrays.sort(concat);
System.out.println(Arrays.toString(concat));

Output:

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



回答7:


There are utility methods for example addALL() method from ArrayUtil class. But what they do is simple concatenate. For your problem you need to write your own logic. For example the following code ensures correct alternate concatenation even if arrays are of unequal length.

    int[] a1 = { 1, 3, 5, 7 };
    int[] a2 = { 2, 4, 6, 8, 9, 10, 122 };
    int totalLen = a1.length + a2.length;
    int[] concat = new int[totalLen];// I made a change here incase the
                                        // arrays are not of equal length
    int i = 0; // this will be the concat array index counter
    int j1 = 0; // this will be the a1 array index counter
    int j2 = 0; // this will be the a2 array index counter
    while (i < totalLen) {

        if ((j1 < a1.length)) {
            concat[i] = a1[j1];
            i++;
            j1++;
        }

        if ((j2 < a2.length)) {
            concat[i] = a2[j2];
            i++;
            j2++;
        }
    }


来源:https://stackoverflow.com/questions/33144667/concatenating-two-arrays-with-alternating-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!