简体   繁体   中英

Shuffle two arrays with same length into one using JAVA

I am trying to combine two arrays with the same length and return an single array with the previous arrays shuffled, but I am not able to return it. What I have tried is the following:

public class PerfectShuffle {
    public static int[]interleave (int[]a1,int[]a2) {
        int[] arrayBla1 = {1, 2, 3};
        int[] arrayBla2 = {4, 5, 6};
        for (int i = 0, j = 0; i < 6 && j < 3; i++, j++) {
            a2[i] = arrayBla1[j];
            i++;
            a2[i] = arrayBla2[j];
        }
        for (int n : a2);
        return a1;
    }
    public static void main (String[]args){
                int[] a1={1, 2, 3};
                int[] a2={4, 5, 6};
            System.out.println(interleave(a1,a2));
    }
}

I commented the code; hope you understand. Keep practising. :-)

import java.util.Arrays;

public class PerfectShuffle {
    public static int[] interleave (int[]a1,int[]a2) {
        // if a1 has more elements than a2 this function will fail; so return a save method coll
        if (a1.length > a2.length)
              return interleave(a2, a1);
        // result array need length combined of both entry arrays
        int[] res = new int[a1.length + a2.length];
        // need a counter j to handle position of result array
        // now loop throw one of both and add alternating elements from both arrays
        for (int i = 0, j = 0; i < a1.length; i++) {
            // add element i from a1 to result array on position j and add 1 to j
            res[j++] = a1[i]; 
            // add element i from a2 to result array on position j and add 1 to j
            res[j++] = a2[i];
        }
        // if a1 < a2: add hangover elements of a2 onto result array
        for (int i = a1.length; i < a2.length; i++) {
            res[a1.length+i] = a2[i];
        }
        return res;
    }

    public static void main (String[]args){
            int[] a1={1, 2, 3};
            int[] a2={4, 5, 6, 7};
            System.out.println(Arrays.toString(interleave(a1,a2)));
    }
}
public static int[] interleave (int[] a1, int[] a2) {
    int[] result = new int[a1.length * 2];
    int j = 0;
    for (int i = 0; i < a1.length; i++) {
        result[j++] = a1[i];
        result[j++] = a2[i];
    }
    return result;
}

public static void main(String[] args) {
    interleave(new int[]{1, 2, 3}, new int[]{4, 5, 6});
}

Step through the above code with a debugger. The array returned is:

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

The above code is based on the assumption that the parameters to method interleave() are equal length arrays, otherwise the above code will not work.

hope it helps.

public static int[]interleave (int[]a1,int[]a2) {
    // you don't need this, because you are passing then in parameters
    //int[] arrayBla1 = {1, 2, 3};
    //int[] arrayBla2 = {4, 5, 6};
    
    // here you gonna have the final array, sized a1 plus a2.
    int[] result = new int[a1.length + a2.length];
    
    // here is how you control the index of the first two arrays, a1 and a2.
    int j = 0;
    int k = 0;

    // When you uses arrays, you have to take care of the length, because the index starts in 0.
    for (int i = 0; i < result.length - 1; i++) {

        //the result will have one item from each array.
        result[i] = a1[j];
        j++;

        //here is the trick, now you increment the result array to receive the data in the index position of the a2 array.
        i++;
        result[i] = a2[k];
        // And here you have to control the index to 'jump ahead' in the a2 array too. The same as we did above.
        k++;

    }
    
    
    return result;
    
}

public static void main (String[]args){
    int[] a1={1, 2, 3};
    int[] a2={4, 5, 6};

    int[] printArray = interleave(a1,a2);

    // You can not print the array using just 'System.out.println' or you will have the default toString() representation of an int array, not the data itself. So you have to iterate over the itens, passing the index and printing it. Now you will have the data.
    for(int i = 0; i < printArray.length; i++) {
        System.out.println(printArray[i]);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM