【问题标题】:How to set/get a value in a two dimensional array list?如何在二维数组列表中设置/获取值?
【发布时间】:2016-09-29 08:41:47
【问题描述】:

我想在二维数组列表中设置/获取一个值。 像这样,

    [0]   [1]   [2]
[0] true  false true
[1] true  true  true
[2] true  false false
[3] false true  true

我的代码 :

 Arraylist<Arraylist<Boolean>> checkBoxState= new Arraylist<Arraylist<Boolean>();

得到:

checkBoxState.get(position).get(h);

设置:

checkBoxState.get(position).set(h,true);
  • 位置、h 是 int 变量

但我的设置/获取代码不起作用。 如何设置/获取 checkBoxState(0.3)?

谢谢。

【问题讨论】:

  • 您面临什么问题?
  • 如何在二维数组列表中获取和添加数据?
  • 你添加了足够的ArrayList&lt;Boolean&gt;checkBoxState 吗?您尝试修改/获取的列表有足够的布尔值吗?
  • 对于外层ArrayList的每个位置,都需要创建一个ArrayList

标签: java android list arraylist


【解决方案1】:

试试这个就行了

        boolean test=true;
        ArrayList<Boolean> mBooleen=new ArrayList<>();
        mBooleen.add(test);
        heckBoxState.add(mBooleen);
        heckBoxState.get(0).set(0, false);
        heckBoxState.get(0).get(0);
        Log.d("your data", heckBoxState.get(0).get(0)+"");

【讨论】:

  • 考虑注释你的代码,不是每个人都能理解的
  • @TilakMadichetti 好的
【解决方案2】:

这样使用

int rowCount = 4, columnCount = 3

private final boolean[][] selectedStatus;

 selectedStatus = new boolean[rowCount][];
        for (int i = 0; i < rowCount; i++) {
            selectedStatus[i] = new boolean[columnCount];

            for (int j = 0; j < columnCount; j++) {
                selectedStatus[i][j] = false;
            }
        }


[0]   [1]   [2]
[0] false false false 
[1] false false false 
[2] true  false false
[3] false false false 

现在通过提供所选行列的值来初始化它:

selectedStatus[0][0] = true;

这将是最初的。并通过提供索引以同样的方式得到它

【讨论】:

    【解决方案3】:

    我认为您面临的问题是填充 ArrayList,试试这个:

         ArrayList<ArrayList<Boolean>> checkBoxState=new ArrayList<ArrayList<Boolean>>(); 
        ArrayList<Boolean> boolList=new ArrayList<Boolean>();
        boolList.add(true);
        boolList.add(true);
        boolList.add(false);
        checkBoxState.add(boolList);
        checkBoxState.add(boolList);
        checkBoxState.add(boolList);
        checkBoxState.get(0).get(2)
    

    【讨论】:

      【解决方案4】:

      当你想使用嵌套的 ArrayList 时,你可以试试这个。我希望它会有所帮助,当您初始化数组时,您也需要填充内部。

      ArrayList<ArrayList<Boolean>> checkBoxState = null;
      
          //filling the arrays you don't have to write boolean again in generics
          for (int i = 0; i < 10; i++) {
              checkBoxState= new ArrayList<>();
              checkBoxState.add(new ArrayList<>());
          }
      
          //You can fill the array how you want
          for (int i = 0; i < checkBoxState.size(); i++) {
              checkBoxState.get(i).add(true);
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-12-13
        相关资源
        最近更新 更多