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 9316517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T02:38:26+00:00 2026-06-19T02:38:26+00:00

I am having problems understanding how deep copying works. I have this 3d vector

  • 0

I am having problems understanding how deep copying works. I have this 3d vector that I wish to copy.

int bands[][][] = new int[parameters.numberPredictionBands + 1][][];
int copy[][][] = new int [parameters.numberPredictionBands + 1][][];

Then I pass this vector to some method that changes bands

prepareBands(bands);

And finally I need to create a deep copy of bands, so when copy is changed bands remains the same and viceversa.

copy = copyOf3Dim(bands, copy);

I’ve tried these different methods but they don’t seem to work for me

Method 1:

private int[][][] copyOf3Dim(int[][][] array, int[][][]copy) {

    for (int x = 0; x < array.length; x++) {  
        for (int y = 0; y < array[0].length; y++) {  
            for (int z = 0; z < array[0][0].length; z++) {
                copy[x][y][z] = array[x][y][z];  
            }  
        }  
    } 
    return copy;
}

Method 2:

private int[][][] copyOf3Dim(int[][][] array, int[][][]copy) {

    for (int i = 0; i < array.length; i++) {

        copy[i] = new int[array[i].length][];
        for (int j = 0; j < array[i].length; j++) {
            copy[i][j] = Arrays.copyOf(array[i][j], array[i][j].length);
        }
    }
    return copy;
}

Method 3:

public int[][][] copyOf3Dim(int[][][] array, int[][][] copy) {

    for (int i = 0; i < array.length; i++) {
        copy[i] = new int[array[i].length][];
        for (int j = 0; j < array[i].length; j++) {
            copy[i][j] = new int[array[i][j].length];
            System.arraycopy(array[i][j], 0, copy[i][j], 0, array[i][j].length);
        }
    }
    return copy;
}    

I think my program crashes when doing array[i].length

Could you please tell me what may I be doing wrong?

  • 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-19T02:38:28+00:00Added an answer on June 19, 2026 at 2:38 am

    A general trick for deep cloning I have successfully used several times is to serialize the object into a ByteArrayOutputStream and then immedieately deserialize it. It’s not a top performer, but it’s a simple two-three lines of code and works to any depth.

    Arrays happen to be Serializable.

    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    new ObjectOutputStream(out).writeObject(array);
    final Spec clone = (int[][][]) 
         new ObjectInputStream(new ByteArrayInputStream(out.toByteArray())).
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this C code that i'm having problems understanding: int foo(int f(int,int), int
I'm having problems understanding how latest.integration works. I have an example that is not
I am relatively new to C++ and am having problems understanding struct. I have
I am having problems understanding what/how this works. it seems odd assigning self to
I am having problems understanding how best to tackle this query. I have a
I'm having problems understanding how preventDefault function works. I created a simple jsfiddle, but
I'm having problems with understanding how jQuery create headers for ajax post. I have
I am new to C programming and I am having problems understanding common pitfalls
I'm having problems understanding how WPF app.xaml works. Is it like Main method in
I'm having problems understanding how I should install xhtml2pdf. I have followed the instructions

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.