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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:10:05+00:00 2026-06-18T06:10:05+00:00

I have been at this little project and i spend like 20 hours solving

  • 0

I have been at this little project and i spend like 20 hours solving (without any luck or results) a major problem in the code. Now i am at the point that i found out that the real problem is that the copy() function does not properly work.

What am i doing wrong?

This is the example i made of the specific problem:

package cloneobject;

import java.util.Arrays;

public class CloneObject {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        clone(new int[3][3]);
    }

    public static void clone(int[][] x) {
        int[][] y = (int[][]) x.clone();
        System.out.println("x=");
        PrintFieldImage(x);
        System.out.println("y=");
        PrintFieldImage(y);
        x[1][1] = 3;        
        System.out.println("x=");
        PrintFieldImage(x);
        System.out.println("y=");
        PrintFieldImage(y);
        y[2][2] = 4;
        System.out.println("x=");
        PrintFieldImage(x);
        System.out.println("y=");
        PrintFieldImage(y);
    }

    public static void PrintFieldImage(int[][] field) {
        if (field != null) {
            int x;
            for (x = 0; x < field.length; x++) {
                System.out.println(Arrays.toString(field[x]));
            }
        } else {
            System.out.println("no field!");
        }
    }
}

and this is the result:

run:
x=
[0, 0, 0]
[0, 0, 0]
[0, 0, 0]
y=
[0, 0, 0]
[0, 0, 0]
[0, 0, 0]
x=
[0, 0, 0]
[0, 3, 0]
[0, 0, 0]
y=
[0, 0, 0]
[0, 3, 0]
[0, 0, 0]
x=
[0, 0, 0]
[0, 3, 0]
[0, 0, 4]
y=
[0, 0, 0]
[0, 3, 0]
[0, 0, 4]
BUILD SUCCESSFUL (total time: 0 seconds)

Now i wish let x contain 3, and y contain 4.

Plz help!

  • 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-18T06:10:06+00:00Added an answer on June 18, 2026 at 6:10 am

    clone() only will do a shallow clone. You are cloning a 2D array; an array of arrays. Only the top level array will be cloned. If you want to do a deep clone, you’ll have to use ol’ fashioned for loop(s).

    int[][] y = new int[x.length][];
    for(int i=0; i<x.length; i++) {
        y[i] = x[i].clone();
    }
    

    See also copy a 2d array in java

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

Sidebar

Related Questions

i have been working on this sub-menu for 8 hours now, the last little
I have been trying to figure this little section of my larger project out
Thus far you guys have been wildly helpful with me getting this little ditty
I have this little question that's been on my mind for a while now.
I have been trying to make this to be a little jQuery plugin that
This is a little different than the questions that have already been asked on
I have been having this annoying problem when trying to implement a picture gallery
I have been using this code to implement a Popup JDialog of sorts, like
I’m having a little Architecture problem. In my project I have a Business Logic
We have been given a little project (As part of my OS course) to

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.