Android:二维ArrayList帮助

编程入门 行业动态 更新时间:2024-10-28 20:24:17
本文介绍了Android:二维ArrayList帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

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

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.

这是我的代码:

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



   }

推荐答案

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

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

这篇关于Android:二维ArrayList帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-16 10:28:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/884881.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Android   ArrayList

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!