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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:09:34+00:00 2026-05-31T17:09:34+00:00

I have the next scenario: I define an int[][] variable in my main class.

  • 0

I have the next scenario:

I define an int[][] variable in my main class. int[][] matrix1 = new int[10][10] and i give it some values. I then call a method and i send this variable as a parameter to that method. Being an object it sends is by reference not by value, so inside the method, because i have to change the values contained by matrix1 but not affect the object after it returns from the method, i make a clone of it like so:

private void myMethod( int[][] matrix1 )
{
    int[][] matrix1Clone = matrix1.clone();
    //And next i do some changes to matrix1Clone
    ......
}

But the problem is that the changes i do to matrix1Clone also happen in matrix1. So it hasn’t really created a clone of matrix1 object, but both variables point to the same object.

Why is this? I can’t seem to figure it out. Why doesn’t clone method work?

If you need more info, please ask. But i’m afraid this is about it, can’t really give you more, but maybe i could try.

I might be missing something, but i can’t figure out what…

Thanks.

EDIT

Sorry, made a typo. It’s late hre and i’m tired. I’m using clone method indeed, that’s why i’m confused as it’s not working :(.

  • 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-31T17:09:35+00:00Added an answer on May 31, 2026 at 5:09 pm

    Try clone it using clone() http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Object.html#clone%28%29

    private void myMethod( int[][] matrix1 )
    {
        int[][] matrix1Clone = matrix1.clone();
    }
    

    or, copy all of the values using a loop

    EDIT: Api for clone() says it should return a copy of the object, but behavior might be different depending on which object’s beeing cloned. Try iterating over the array as an alternative. Since it’s a 2d array, you need a nested loop:

    for(int i=0; i<old.length; i++)
      for(int j=0; j<old[i].length; j++)
        old[i][j]=copy[i][j];
    

    where old is the “original array” and copy is the copy

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

Sidebar

Related Questions

I have next tables Order , Transaction , Payment . Class Order has some
I have next package structure webService.ear -- dependency.jar -- some.class Can I remove some.class
I have the following scenario: class Task { int id; Group group; User user;
How could I best implement the following scenario? I have a Next Period... button
Scenario: You have an ASP.Net webpage that should display the next image in a
I have next code require 'rack/rpc' class Server < Rack::RPC::Server def hello_world Hello, world!
I have next problem: i have a View: <UserControl x:Class=WpfApplication1.UserControl1 xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml xmlns:mc=http://schemas.openxmlformats.org/markup-compatibility/2006 xmlns:d=http://schemas.microsoft.com/expression/blend/2008
So here's the scenario: I have a Spring XML configuration with some lazy-beans, some
Here is the scenario... I have a a checkbox next to each field that
I have a common scenario that I am looking for some guidance from people

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.