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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:04:52+00:00 2026-06-17T05:04:52+00:00

I’m having trouble working with multidimensional strings arrays and would appreciate any clarity. I’ll

  • 0

I’m having trouble working with multidimensional strings arrays and would appreciate any clarity.

I’ll post a sample code below that I wrote just to play around with to get an idea of how multidimensional string arrays work. I wrote in comments what I expect to be happening and what I expect the result to be, but isn’t. My questions is pretty much why isn’t it working the way I am thinking, it should create the matrix
[1][2][3]

[4][5][6]

Also an additional question, if I have the 2×3 martrix already setup, how can I reassign the value in a specific element, for example [1][1], or do I have to call the row and assign it to a variable. This whole sample code is for me to learn how to assign elements in a multidimensional string array and then reassign them. These string array lists, I do not know the dimension ahead of time and the values change, there I need to use the .add method and eventually .set.

Thank you in advance for any help, amazing community it seems to be at Stack Overflow

TextView displayanswer;
String text0, text1, text2, text3, text4, text5, text6;
ArrayList<String> column = new ArrayList<String>();
ArrayList<ArrayList<String>> row = new ArrayList<ArrayList<String>>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.test1);
    displayanswer = (TextView) findViewById(R.id.textView1);

    //Creates Matrix in column variable of [1][2][3]
    column.add("1");
    column.add("2");
    column.add("3");  

    row.add(column);  //Creates Matrix in row variable of [1][2][3]
    row.add(column);  //Creates 2 dimension matrix of  [1][2][3]
                     //                                [1][2][3]

    column.set(0, "4"); //column variable Matrix becomes  [4][2][3]
    column.set(1, "5"); //column variable Matrix becomes  [4][5][3]
    column.set(2, "6"); //column variable Matrix becomes  [4][5][6]
    row.set(1,column);  //2 dimensional matrix becomes  [1][2][3]
                                                                  //[4][5][6]

           column = row.get(0); //get first row of 2 row matrix, should be[1][2][3]

    //Assigning first row elements to text strings
    text0 = column.get(0); text1 = column.get(1); text2 = column.get(2);

    column = row.get(1); //gets second row of 2 row matrix, should be [4][5][6]

    //Assigning second row elements to text strings
    text3 = column.get(0); text4 = column.get(1); text5 = column.get(2);

    //should give me 123456 but instead I get 456456???
    displayanswer.setText(""+text0 +text1 +text2 +text3 +text4 +text5);

}

}

  • 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-17T05:04:55+00:00Added an answer on June 17, 2026 at 5:04 am

    I guess you’re making the assumption that when you add something to a list, it creates a copy of the data. However, that’s not the case. The list will contain a reference to the data you’re adding, not (a copy of) the actual content itself.

    So even though the comments below are correct, because you’re adding the ‘column’ object twice …

    row.add(column);  //Creates Matrix in row variable of [1][2][3]
    row.add(column);  //Creates 2 dimension matrix of  [1][2][3]
    

    … both list entries will refer to the same object. Then, by attempting to change the values of one column, you’re actually changing the referenced object and thus you’re changing both entries. That’s why you end up with “456456”.

    This will actually affect both columns, since the same object is referenced on the first two positions:

    column.set(0, "4"); //column variable Matrix becomes  [4][2][3]
    column.set(1, "5"); //column variable Matrix becomes  [4][5][3]
    column.set(2, "6"); //column variable Matrix becomes  [4][5][6]
    

    The following is not true:

    row.set(1,column);  //2 dimensional matrix becomes  [1][2][3]
                                                                  //[4][5][6]
    

    Both entries change (because both refer to the same object), hence you end up with:

    [4][5][6]
    [4][5][6]
    

    To ‘fix’ this, make sure you create a new ‘column’ object before adding the second column. Alternatively, if you prefer to keep modifying the same object, make sure you add a copy of the data to the list. For example:

    //Creates Matrix in column variable of [1][2][3]
    column.add("1");
    column.add("2");
    column.add("3");
    
    row.add(new ArrayList<String>(column));
    row.add(new ArrayList<String>(column));
    

    After that, you can safely change column without affecting the content of the list. You can also manipulate the list content directly, simply because we added copies of the data as rows, so every entry points a different object. Hope that makes sense.

    On a side note: I’d probably consider creating an actual Matrix class if you’re planning on doing more complex things. Lists aren’t particularly ideal for representing a matrix. Alternatively there are probably heaps of matrix implementations around you can learn from or borrow. Just a thought. 🙂

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have this code to decode numeric html entities to the UTF8 equivalent character.
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Does anyone know how can I replace this 2 symbol below from the string

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.