Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 定义多维arraylist的容量_Java_Arraylist_Multidimensional Array - Fatal编程技术网

Java 定义多维arraylist的容量

Java 定义多维arraylist的容量,java,arraylist,multidimensional-array,Java,Arraylist,Multidimensional Array,我已经创建了一个名为Grid的类,我正在努力定义嵌套ArrayList的容量。这就是我目前拥有的: public class Grid extends GameObject { private int cols; private int rows; private int colWidth; private int rowHeight; private ArrayList<ArrayList<GameObject>> contents;

我已经创建了一个名为
Grid
的类,我正在努力定义嵌套
ArrayList
的容量。这就是我目前拥有的:

public class Grid extends GameObject {

   private int cols;
   private int rows;
   private int colWidth;
   private int rowHeight;
   private ArrayList<ArrayList<GameObject>> contents;

   public Grid(int x, int y, int cols, int rows, int colWidth, int rowHeight, ID id) {
       super();
       this.x = x;
       this.y = y;
       this.cols = cols;
       this.rows = rows;
       this.colWidth = colWidth;
       this.rowHeight = rowHeight;

       //Here I want to define the contents

       this.width = colWidth * cols;
       this.height = rowHeight * rows;
       this.id = id;
   }
 }
公共类网格扩展游戏对象{
私人公司;
私有int行;
私有int-colWidth;
私人室内高度;
私有数组列表内容;
公共网格(int x,int y,int cols,int rows,int colWidth,int rowHeight,ID){
超级();
这个.x=x;
这个。y=y;
this.cols=cols;
this.rows=行;
this.colWidth=colWidth;
this.rowHeight=rowHeight;
//我想在这里定义内容
this.width=colWidth*cols;
this.height=行高*行;
this.id=id;
}
}
代码应该如下所示:

this.contents = new ArrayList<ArrayList<GameObject>(cols)>(rows);
this.contents=新的ArrayList(行);

但这是一个错误。如果有人知道如何解决这个问题,我将不胜感激!提前谢谢

创建列表时定义大小

contents = new ArrayList<>(x);

contents.add(new ArraysList<GameObject>(y));
contents=newarraylist(x);
内容。添加(新阵列列表(y));

创建列表时定义大小

contents = new ArrayList<>(x);

contents.add(new ArraysList<GameObject>(y));
contents=newarraylist(x);
内容。添加(新阵列列表(y));

一条初始化语句无法完成此操作。你需要一个循环

this.contents = new ArrayList<ArrayList<GameObject>>(rows); // this creates an empty 
                                                            // ArrayList
for (int i = 0; i < rows; i++) { // this populates the ArrayList with rows empty ArrayLists
    this.contents.add(new ArrayList<GameObject>(cols));
    // and possibly add another loop to populate the inner array lists
}
this.contents=newarraylist(行);//这将创建一个空的
//ArrayList
对于(int i=0;i
一条初始化语句无法完成此操作。你需要一个循环

this.contents = new ArrayList<ArrayList<GameObject>>(rows); // this creates an empty 
                                                            // ArrayList
for (int i = 0; i < rows; i++) { // this populates the ArrayList with rows empty ArrayLists
    this.contents.add(new ArrayList<GameObject>(cols));
    // and possibly add another loop to populate the inner array lists
}
this.contents=newarraylist(行);//这将创建一个空的
//ArrayList
对于(int i=0;i
从应用程序的角度来看,您根本无法做到这一点,因为它是一个一维列表。另外,
newarraylist(N)
没有定义列表的最大容量(就像
newgameobject[N]
会定义的),但它定义了它的初始容量。在向该列表添加N个元素之后,您仍然可以添加更多元素,因为将在内部分配另一个数组,这次比N大,并且内容将被复制


您需要查看每个维度,并创建具有可选初始容量集的新列表

从应用程序的角度来看,您根本无法做到这一点,因为它是单维度列表。另外,
newarraylist(N)
没有定义列表的最大容量(就像
newgameobject[N]
会定义的),但它定义了它的初始容量。在向该列表添加N个元素之后,您仍然可以添加更多元素,因为将在内部分配另一个数组,这次比N大,并且内容将被复制


您需要查看每个维度,并创建具有可选初始容量集的新列表

我有点希望这是可能的,但这将不得不做。谢谢你!我有点希望这是可能的,但这将不得不做。谢谢你!