【问题标题】:Java - Is ArrayList<Integer>[][] possible?Java - ArrayList<Integer>[][] 可能吗?
【发布时间】:2011-10-12 12:09:42
【问题描述】:
ArrayList<Integer>[][] matrix = new ArrayList<Integer]>[sizeX][sizeY]();

ArrayList<Integer>[][] matrix = new ArrayList<Integer]>()[sizeX][sizeY];

不工作,我开始认为将 ArrayLists 存储在矩阵中是不可能的?

【问题讨论】:

  • 你想达到什么目的?您发布的两件事显然是不可能的,因为它们无法编译。你能告诉我们你想要什么吗?

标签: java arrays matrix arraylist


【解决方案1】:

如果还想用and数组:

    ArrayList<Integer>[][] matrix = new ArrayList[1][1];
    matrix[0][0]=new ArrayList<Integer>();
    //matrix[0][0].add(1);

【讨论】:

    【解决方案2】:

    试试

    List<List<Integer>> twoDList = new ArrayList<ArrayList<Integer>>();
    

    阅读更多List

    【讨论】:

    • 我要做的是创建一个多维数组(矩阵),其中每个位置都有一个 ArrayList。
    • 很难确定,但听起来 OP 实际上想要一个 3d 矩阵:一个 2d 矩阵,其单元格是数组列表。
    【解决方案3】:

    用这个,

    List<List<Integer>> matrix = new ArrayList<ArrayList<Integer>>();  
    

    这意味着您的列表将由整数列表组成。

    【讨论】:

      【解决方案4】:

      泛型和数组通常不能很好地混合,但这会起作用(发出警告,可以安全地忽略):

      ArrayList<Integer>[][] matrix = new ArrayList[sizeX][sizeY];
      

      【讨论】:

        【解决方案5】:

        如果你想将一个列表存储在一个数组中,那么你仍然需要将声明和初始化分开:

        ArrayList<Integer>[][] matrix = new ArrayList[10][10];
        

        将指定 ArrayList 对象的二维数组。

        matrix[0][0] = new ArrayList<Integer>();
        

        将使用新的整数 ArrayList 初始化一个特定单元格。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-06-22
          • 2011-07-09
          • 1970-01-01
          • 2011-01-08
          相关资源
          最近更新 更多