Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8887691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:52:22+00:00 2026-06-14T21:52:22+00:00

I have yet another Java question :) I have read this thread, where it

  • 0

I have yet another Java question 🙂

I have read this thread, where it explains it clearly, but I have two bi-dimensional arrays that I would like to copy.

I understand that this piece of code

int[] array1and2 = new int[array1.length + array2.length];
System.arraycopy(array1, 0, array1and2, 0, array1.length);
System.arraycopy(array2, 0, array1and2, array1.length, array2.length);

But my question is, how do I merge it with two arrays where

int a1[][] = new int [3][3];
int b1[][] = new int [3][3];
int c1[][] = new int [3][6];

Where c1 is the merging of aforementioned arrays?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T21:52:23+00:00Added an answer on June 14, 2026 at 9:52 pm

    Use solution from task, that You have mentioned in the question. Example:

    import java.util.Arrays;
    
    public class ArrayProgram {
    
        public static void main(String[] args) {
            int[][] array1 = { { 1, 2, 3 }, { 1, 2, 3 }, { 1, 2, 3 } };
            int[][] array2 = { { 4, 5, 6 }, { 7, 8, 9 }, { 0, 1, 2 } };
            int[][] result = ArrayCopier.joinSecondDimension(array1, array2);
            for (int[] array : result) {
                System.out.println(Arrays.toString(array));
            }
        }
    }
    
    class ArrayCopier {
    
        public static int[][] joinSecondDimension(int[][] array1, int[][] array2) {
            int[][] array1and2 = new int[array1.length][];
            for (int index = 0; index < array1.length; index++) {
                array1and2[index] = join(array1[index], array2[index]);
            }
            return array1and2;
        }
    
        public static int[] join(int[] array1, int[] array2) {
            int[] array1and2 = new int[array1.length + array2.length];
            System.arraycopy(array1, 0, array1and2, 0, array1.length);
            System.arraycopy(array2, 0, array1and2, array1.length, array2.length);
            return array1and2;
        }
    }
    

    Prints:

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

    EDIT
    Implementation for any arguments number (Variable-Length Argument Lists):

    import java.util.Arrays;
    
    public class ArrayProgram {
    
        public static void main(String[] args) {
            int[][] array1 = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
            int[][] array2 = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
            int[][] array3 = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
            test(array1);
            test(array1, array2);
            test(array1, array2, array3);
        }
    
        private static void test(int[][]... arrays) {
            int[][] result = ArrayCopier.joinSecondDimension(arrays);
            for (int[] array : result) {
                System.out.println(Arrays.toString(array));
            }
            System.out.println();
        }
    }
    
    class ArrayCopier {
    
        public static int[][] joinSecondDimension(int[][]... arrays) {
            int firstArrayLength = arrays[0].length;
            int[][] result = new int[firstArrayLength][];
            for (int index = 0; index < firstArrayLength; index++) {
                result[index] = join(getSecondDimArrays(index, arrays));
            }
            return result;
        }
    
        public static int[] join(int[]... arrays) {
            int[] result = new int[getTotalLength(arrays)];
            int destPos = 0;
            for (int[] array : arrays) {
                System.arraycopy(array, 0, result, destPos, array.length);
                destPos += array.length;
            }
            return result;
        }
    
        private static int getTotalLength(int[]... arrays) {
            int length = 0;
            for (int[] array : arrays) {
                length += array.length;
            }
            return length;
        }
    
        private static int[][] getSecondDimArrays(int index, int[][]... arrays) {
            int[][] result = new int[arrays.length][];
            int resultIndex = 0;
            for (int[][] array : arrays) {
                result[resultIndex++] = array[index];
            }
            return result;
        }
    }
    

    Prints:

    [1, 2, 3]
    [4, 5, 6]
    [7, 8, 9]
    
    [1, 2, 3, 1, 2, 3]
    [4, 5, 6, 4, 5, 6]
    [7, 8, 9, 7, 8, 9]
    
    [1, 2, 3, 1, 2, 3, 1, 2, 3]
    [4, 5, 6, 4, 5, 6, 4, 5, 6]
    [7, 8, 9, 7, 8, 9, 7, 8, 9]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Yes, this is yet another question on producer/consumer in Java. My variant is that
Okay this may be a simple question but I have yet to come with
I'm relatively new to concurrency in Java (still have yet to read JCIP, but
Apologies for posting yet another Java jar question. I did have a look at
I have searched online for a solution, but have yet to find one that
This question concerns the reflection mechanisms of the java programming language. I have an
Suppose I have a Java servlet that I want to use for two (or
Hello good people of Stack Overflow, I have come with yet another question for
I need help with a question that I could not answer yet. I have
I have a web-application in java which is invoked by yet another web-application written

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.