0

I have 3 arrays A, B and C. A and B have a size of 6 and C has a size of 12. I have to combine A and B into C but using the first 3 integers of A, then the first 3 integers of B, then the next 3 integers from A and lastly the last 3 integers from B. For example: int A[]={1,2,3,7,8,9}, B[]={4,5,6,10,11,12} and filling C with C[]={1,2,3,4,5,6,7,8,9,10,11,12}

Here's my code so far:

   public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int[] A = new int[6];
    int[] B = new int[6];
    int[] C = new int[12];

    int count = 0;

    for (int i = 0; i < 6; i++) {
        System.out.println("Array A: ");
        A[i]=sc.nextInt();
        System.out.println("Array B: ");
        B[i]=sc.nextInt();
    }

    System.out.println("");
    System.out.print("|");
    for (int i = 0; i < 6; i++) {
        System.out.print(A[i]+"|");
    }
    System.out.println("");
    System.out.print("|");
    for (int i = 0; i < 6; i++) {
        System.out.print(B[i]+"|");
    }

    while(count < 3){
    count++;
    {

I'm really lost with this one

Rene Vazquez
  • 13
  • 1
  • 4
  • From your code, it looks like you can have many `for-loops`. In your first `for-loop`, loop from 0 to 2 storing the first 3 from the first array. In your second `for-loop`, loop from 0 to 2 storing the first 3 from the second array. In your third `for-loop`, loop from 3 to 5 storing the last 3 in the first index and so on. – SedJ601 Oct 01 '18 at 21:46
  • Start by trying to accomplish just the first step: copy the first three elements of `A` to the first three positions of `C`. – Kevin Anderson Oct 01 '18 at 21:57

3 Answers3

1

This is a relatively simple problem that can be solved in a few ways. The simplest would be, as this is a static example you gave, to do something like

    c[0] = a[0];
    c[1] = a[1];
    c[2] = a[2];
    c[3] = b[0];
         .
         .
         .
    c[11] = b[5]

This does not scale very well, and is difficult to maintain, but technically does what you ask.

Next, I would look at simply for loops. In a previous version of this post, I did not include them as I saw other responses with them, but have since added this in as I thought they could be improved:

    //  Using loops     
    final int[] c = new int[12];
    for (int i = 0; i < 3; ++i) {
        c[i] = a[i];
        c[i + 3] = b[i];
        c[i + 6] = a[i + 3];
        c[i + 9] = b[i + 3];
    }

This is also very simple and efficient. We only need 3 iterations of the loop to cover both halves of the array, using offsets to avoid having to make multiple for loops for each part. Knowledge for this kind of simple comes through experience, so I'd recommend working through some more examples.

Next, we will look at some more fun options. If you are not familiar with the Java API, I would recommend first googling various things such as "java copy part of array" or "java insert array into another array".

Such searches lead to posts like:

Java copy section of array

Insert array into another array

How to use subList()

From there, you can construct an reasonably well defined answer such as:

    //  Using System::arraycopy<T>(T[], int, T[], int, int), where T is the array-type.
    final int[] c = new int[12];
    System.arraycopy(a, 0, c, 0, 3);
    System.arraycopy(b, 0, c, 3, 3);
    System.arraycopy(a, 3, c, 6, 3);
    System.arraycopy(b, 3, c, 9, 3);

Which is probably sufficient for most of your needs.

But, going through that process, I learnt an additional way to do it with streams!

    // Using java.util.stream
    final List<Integer> l_a = Arrays.stream(a).boxed().collect(Collectors.toList());
    final List<Integer> l_b = Arrays.stream(b).boxed().collect(Collectors.toList());

    final List<Integer> l_c = new ArrayList<Integer>() {{
        addAll(l_a.subList(0, 3));
        addAll(l_b.subList(0, 3));
        addAll(l_a.subList(3, 6));
        addAll(l_b.subList(3, 6));
    }};

    final int[] c = l_c.stream().mapToInt(i -> i).toArray();

And though this last example is perhaps less elegant than the second, going through the process of researching the problem and potential solutions has taught me something I can now carry with me going forward.

Hopefully this helps!

Zackary
  • 195
  • 9
0
int[] A = new int[]{1,2,3,7,8,9};
int[] B = new int[]{4,5,6,10,11,12};
int[] C = new int[12];

int aIndex= 0;
int bIndex= 0;
for(int i=0; i<C.length; i++) {
    if((i >= 0 && i < 3) || (i >= 6 && i < 9))
        C[i] = A[aIndex++];
    else
        C[i] = B[bIndex++];
}
0

Here is some sample code that will solve your problem:

int[] a = {1,2,3,7,8,9};
int[] b =  {4,5,6,10,11,12};
int[] c = new int[12];
for(int x = 0; x < c.length; x++) {
    if(x <= 2) {
       c[x] = a[x];
       }
    else if(x >= 3 && x<=5) {
            c[x] = b[x-3];
        }
    else if (x >= 6 && x <= 8) {
        c[x] = a[x -3];
        }
    else if(x>=6 && x<=11) {
        c[x] = b[x -6];

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

We can iterate through the array so long as x <= 2 in order to get the first three indexes of the a array. We can then iterate through the first three indexes of the b array so long as x is >= 3 && <= 5. We can then iterate through the last three values of a, and then the last three values of b. We have to be sure to do "b[x-6]" because x will clearly be more than the number of index that we have in the b array. The output will be as follows:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
Simeon Ikudabo
  • 2,152
  • 1
  • 10
  • 27