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);
}
}
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 …
… 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:
The following is not true:
Both entries change (because both refer to the same object), hence you end up with:
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:
After that, you can safely change
columnwithout 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
Matrixclass 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. 🙂