Android:需要帮助修复在ArrayList中添加元素的循环

时间:2021-11-10 07:16:58

Currently I have a two dimensional ArrayList of EditTexts. Currently if a user enters in a 2x3 matrix (based on what is entered into the EditTexts) such as

目前我有一个EditTexts的二维ArrayList。目前,如果用户输入2x3矩阵(基于输入到EditTexts中的内容),例如

[1 2 3]  
[4 5 6]

my code would create a Two-Dimensional ArrayList like below instead of like above

我的代码会像下面那样创建一个二维ArrayList而不是像上面那样

[1 2 3 4 5 6]  
[1 2 3 4 5 6]

How do I get my code to produce an array like the first one instead of what it currently is producing?

如何让我的代码生成像第一个数组而不是它当前生成的数组?

My code:

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

   @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);
           arrayOfEditTexts.add(columnEditTexts);
       }

   }

1 个解决方案

#1


0  

Your nested loop is adding elements to the same arraylist (columnEditTexts) on each iteration, while your outer loop is adding the same columnEditTexts to arrayOfEditTexts. You need to set columnEditTexts to a new instance of ArrayList at the beginning of the outer loop.

您的嵌套循环在每次迭代时向同一个arraylist(columnEditTexts)添加元素,而您的外部循环将相同的columnEditTexts添加到arrayOfEditTexts。您需要在外部循环的开头将columnEditTexts设置为ArrayList的新实例。

Change 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++){

to this:

   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++){

#1


0  

Your nested loop is adding elements to the same arraylist (columnEditTexts) on each iteration, while your outer loop is adding the same columnEditTexts to arrayOfEditTexts. You need to set columnEditTexts to a new instance of ArrayList at the beginning of the outer loop.

您的嵌套循环在每次迭代时向同一个arraylist(columnEditTexts)添加元素,而您的外部循环将相同的columnEditTexts添加到arrayOfEditTexts。您需要在外部循环的开头将columnEditTexts设置为ArrayList的新实例。

Change 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++){

to this:

   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++){