java Android:二维ArrayList帮助

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5176512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 09:50:28  来源:igfitidea点击:

Android: Two dimensional ArrayList Help

javaandroidmultidimensional-arrayarraylist

提问by Biggsy

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.

目前我的代码将用户输入放入一维 ArrayList,但我想将它们放入二维 ArrayList 并且遇到了一些麻烦。

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);
       }



   }

回答by Corey Sunwold

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

那么你需要先创建一个二维的ArrayList。为此,您需要创建一个 ArrayList 的 ArrayList。

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);
   }