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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T13:32:29+00:00 2026-05-11T13:32:29+00:00

I’m working with a program that uses two-dimensional arrays of Strings (probably not that

  • 0

I’m working with a program that uses two-dimensional arrays of Strings (probably not that smart to begin with, but eh), and I’d like to write a function that takes one of these arrays (let’s say array1), makes an independent copy, and returns it (let’s say array2). However, when I then change a value in array2, it seems to be reflected in array1.

My function currently looks something like this:

public static String[][] copy(String[][] matrix, int n) {     String[][] out = new String[n+1][n+1];     for (int i = 0; i < n+1; i++)          for (int j = 0; j < n+1; j++) {             if(matrix[i][j] != null) {                 String cp = new String(matrix[i][j]);                 out[i][j] = cp;             }          }      return out; } 

I declare a new array of Strings, and then iterate through it, copying each value individually. When that didn’t work, I even tried explicitly declaring a new string from each old string and putting that in the array instead.

Can anyone tell me where I’m going 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. 2026-05-11T13:32:30+00:00Added an answer on May 11, 2026 at 1:32 pm

    Your method looks like it should work, though passing in n as a parameter makes it brittle, using the input array’s length field would be better, and you could even handle jagged arrays that way.

    Making a copy of the contents is not necessary, since Strings cannot be changed – which leads to the main question: What kind of changes are you making that seem to be reflected in the copy? Show us the code that does this.

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

Sidebar

Ask A Question

Stats

  • Questions 130k
  • Answers 130k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Your title asked for a string, but the question text… May 12, 2026 at 6:04 am
  • Editorial Team
    Editorial Team added an answer Number of problems can cause this: Did you set a… May 12, 2026 at 6:04 am
  • Editorial Team
    Editorial Team added an answer Actually, it turned out to be a path issue. I… May 12, 2026 at 6:04 am

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.