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

  • Home
  • SEARCH
  • 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 7636761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:46:00+00:00 2026-05-31T07:46:00+00:00

I have defined an int[][] object. Because it is an object, if i send

  • 0

I have defined an int[][] object. Because it is an object, if i send it to a method as a parameter, it will only send it’s reference, so any changes to the array in the method, will influence it in the main program. So i would like to make a clone of this object inside the method, but i’m not sure how to accomplish this.

I was thinking of something like so:

private void myMethod( int[][] array )
{
    //Define our temporary array (clone)
    int[][] newArray = new int[3][3];

    //Go through the elements of the array
    for .... row = 0; row < ..; row++
        for ..... col = 0; col < ..; col++
           //Copy individual elements from one array to another
           newArray[row][col] = array[row][col];
}

but will the above code copy each element from array into newArray as value (so… a clone of the item), or just the reference?

If so, how can this be accomplished. If i were to use ArrayLists instead of int[][] objects, there is the clone() method or something like that, but i haven’t got that method for int[][] objects 🙁

Also, if i’m not mistaken if i do this inside the method newArray = array , that will copy just the reference again, so both will point to the same int[][] object 🙁

P.S. I know i could just test this, but i’d like to discuss it with you guys a bit, and see what’s what exactly.

  • 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-05-31T07:46:01+00:00Added an answer on May 31, 2026 at 7:46 am

    You could use clone() on the matrix and on each array corresponding to a row in the matrix, it will work without problems because you’re cloning a matrix of primitive values, like this:

    int[][] matrix = new int[3][3];
    // ... matrix gets filled ...
    int[][] copy = matrix.clone();
    for (int i = 0; i < matrix.length; i++)
        copy[i] = matrix[i].clone();
    

    The above will create a copy matrix which is independent of matrix, meaning that you can change the values of the copy without affecting the original.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small custom object defined as: public class TimeSeriesDefinition { public int
I have defined a struct ABC to contain an int ID, string NAME, string
Let us suppose i have these three methods defined: int F1(int, int); int F1(float,
I have a private const int defined in a C# class. I want compilation
I have a class defined as: class Obj { public: int width, height; Obj(int
I have this user defined function. public partial class UserDefinedFunctions { static int i;
I have a struct defined as: struct { char name[32]; int size; int start;
I have a function pointer defined by: typedef void (*EventFunction)(int nEvent); Is there a
I have an SQL table defined as below: CREATE TABLE [TestComposite] ( ID int,
I have an inline member function defined under class MyClass int MyClass::myInlineFunction(); This function

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.