4

Currently I have my code putting user input into a one-dimensional ArrayList, but I would like to put them into a two dimensional ArrayList and am having some trouble.

Here is my code:

public class Game extends Activity implements OnClickListener {
   private static final String TAG = "Matrix";
   static ArrayList<EditText> columnEditTexts;




   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       this.setContentView(R.layout.matrix);
       View doneButton = findViewById(R.id.done_button);
       doneButton.setOnClickListener(this);
       columnEditTexts = new ArrayList<EditText>();

       for(int i = 0; i < MatrixMultiply.h1; i++){
           TableLayout table = (TableLayout)findViewById(R.id.myTableLayout);
           TableRow row = new TableRow(this);
           EditText column = new EditText(this);
           for(int j = 0; j < MatrixMultiply.w1; j++){
               table = (TableLayout)findViewById(R.id.myTableLayout);
               column = new EditText(this);
               column.setId(i);
               row.addView(column);
               columnEditTexts.add(column);
           }
           table.addView(row);
       }



   }
4
  • and what, exactly, is the trouble? Commented Mar 3, 2011 at 4:13
  • Right now it's only putting the EditTexts into a single dimension array. How would I put them into a two dimensional array? Commented Mar 3, 2011 at 4:23
  • Could you add to the question what you hope to achieve by storing these in a two dimensional array? Commented Mar 3, 2011 at 4:32
  • Ultimately I would like to Multiply two matrices together (for example roseindia.net/java/beginners/MatrixMultiply.shtml). I have another activity for the second matrix that is just like this one and then a results activity. Commented Mar 3, 2011 at 4:43

1 Answer 1

9

Well you need to first create a two dimensional ArrayList. To do that, you need to create an ArrayList of ArrayLists.

ArrayList<ArrayList<EditText>> arrayOfEditTexts = new ArrayList<ArrayList<EditText>>();

So then you loop will become something along these lines (assuming I understand what you are trying to do):

for(int i = 0; i < MatrixMultiply.h1; i++){
       columnEditTexts = new ArrayList<EditText>();
       TableLayout table = (TableLayout)findViewById(R.id.myTableLayout);
       TableRow row = new TableRow(this);
       EditText column = new EditText(this);
       for(int j = 0; j < MatrixMultiply.w1; j++) {               
           column = new EditText(this);
           column.setId(i);
           row.addView(column);
           columnEditTexts.add(column);
       }
       table.addView(row);
       arrayOfEditTexts.add(columnEditTexts);
   }
Sign up to request clarification or add additional context in comments.

2 Comments

This is close but seems to keep adding identical ArrayLists. For a 2x3 matrix I would like the ArrayList to be [1 2 3] [4 5 6]. Currently it is storing it as [1 2 3 4 5 6] [1 2 3 4 5 6]
There is a problem in your for loop then. The structure of the ArrayList remains the same, the problem is in how you populate it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.