Java 如何动态增加二维阵列的大小
如果我知道有多少元素,我已经知道如何制作固定数组。例如,对于7个元素,我做了一些类似于int数组[2][4]的事情 但是如果我在开始时有0个元素(这意味着数组在开始时是空的),并且希望在程序运行时增加数组大小,该怎么办Java 如何动态增加二维阵列的大小,java,arrays,Java,Arrays,如果我知道有多少元素,我已经知道如何制作固定数组。例如,对于7个元素,我做了一些类似于int数组[2][4]的事情 但是如果我在开始时有0个元素(这意味着数组在开始时是空的),并且希望在程序运行时增加数组大小,该怎么办 基本上,如何添加新行或列?创建一个更大的新数组,复制现有元素并添加要添加的元素。您可以使用类似ArrayList的东西,但这很昂贵,并且将使用大约4倍的内存。如果你不想自己调整数组的大小,我会考虑使用 TyDeLayRayList。< /P> < P>创建一个更大的新数组,复制现
基本上,如何添加新行或列?创建一个更大的新数组,复制现有元素并添加要添加的元素。您可以使用类似ArrayList的东西,但这很昂贵,并且将使用大约4倍的内存。如果你不想自己调整数组的大小,我会考虑使用<代码> TyDeLayRayList。< /P> < P>创建一个更大的新数组,复制现有元素并添加要添加的元素。您可以使用类似ArrayList的东西,但这很昂贵,并且将使用大约4倍的内存。如果你不想自己调整数组的大小,我会考虑使用<代码> TyDeLayRayList。< /P> < P>创建一个更大的新数组,复制现有元素并添加要添加的元素。您可以使用类似ArrayList的东西,但这很昂贵,并且将使用大约4倍的内存。如果你不想自己调整数组的大小,我会考虑使用<代码> TyDeLayRayList。< /P> < P>创建一个更大的新数组,复制现有元素并添加要添加的元素。您可以使用类似ArrayList的东西,但这很昂贵,并且将使用大约4倍的内存。如果您不想自己调整数组的大小,我会考虑使用<代码> TyDeLayRayList。< /P>
文档,以及。享受吧
具体而言:ArrayList<Integer> list = new ArrayList<Integer>();
list.add(1); // Add 1 to the list.
ArrayList list=new ArrayList();
列表。添加(1);//将1添加到列表中。
文档,以及。享受吧
具体而言:
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(1); // Add 1 to the list.
ArrayList list=new ArrayList();
列表。添加(1);//将1添加到列表中。
文档,以及。享受吧
具体而言:
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(1); // Add 1 to the list.
ArrayList list=new ArrayList();
列表。添加(1);//将1添加到列表中。
文档,以及。享受吧
具体而言:
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(1); // Add 1 to the list.
ArrayList list=new ArrayList();
列表。添加(1);//将1添加到列表中。
Java数组可以动态创建,但不能动态扩展。你可能想看看或其他建议
但是,请记住,在您的示例中,您创建了一个矩阵,这是一件稍有不同的事情。Java数组可以动态创建,但不能动态扩展。你可能想看看或其他建议
但是,请记住,在您的示例中,您创建了一个矩阵,这是一件稍有不同的事情。Java数组可以动态创建,但不能动态扩展。你可能想看看或其他建议
但是,请记住,在您的示例中,您创建了一个矩阵,这是一件稍有不同的事情。Java数组可以动态创建,但不能动态扩展。你可能想看看或其他建议
但是,请记住,在您的示例中,您创建了一个稍微不同的矩阵。请注意,
newint[2][4]
是一个int[]
数组;int[]
数组中的int[]
数组最初都是相同的长度,但不要求它们保持相同的长度。int[][]
数组的任何元素都可以重新分配长度不同的int[]
,而不会影响其他元素。“行”和“列”的概念是Java数组不支持的更高层次的概念
按照其他答案的建议使用ArrayList
不会改变这一点。此外,根据使用ArrayList
的方式,由于将int
值自动装箱为Integer
对象,可能会导致相当大的开销
如果您想保留数据的矩形形状,我建议您定义一个保持所有维度一致的Matrix
类。(或者,更好的方法是将二维数组线性化为一维数组,并使用内部存储的行和列大小进行适当的下标计算。或者,最好使用编写良好的矩阵库,如或基本集合库等。)
编辑下面是一个简单矩阵类的开始,该类在内部使用线性存储方案,并允许调整矩阵大小。数据存储在中,索引基于0
public class IntMatrix {
private int rows;
private int cols;
private int[] data;
/**
* Allocate a matrix with the indicated initial dimensions.
* @param cols The column (horizontal or x) dimension for the matrix
* @param rows The row (vertical or y) dimension for the matrix
*/
public IntMatrix(int cols, int rows) {
this.rows = rows;
this.cols = cols;
data = new int[cols * rows];
}
/**
* Calculates the index of the indicated row and column for
* a matrix with the indicated width. This uses row-major ordering
* of the matrix elements.
* <p>
* Note that this is a static method so that it can be used independent
* of any particular data instance.
* @param col The column index of the desired element
* @param row The row index of the desired element
* @param width The width of the matrix
*/
private static int getIndex(int col, int row, int width) {
return row * width + col;
}
public int get(int col, int row) {
return data[getIndex(col, row, cols)];
}
public void set(int col, int row, int value) {
data[getIndex(col, row, cols)] = value;
}
/**
* Resizes the matrix. The values in the current matrix are placed
* at the top-left corner of the new matrix. In each dimension, if
* the new size is smaller than the current size, the data are
* truncated; if the new size is larger, the remainder of the values
* are set to 0.
* @param cols The new column (horizontal) dimension for the matrix
* @param rows The new row (vertical) dimension for the matrix
*/
public void resize(int cols, int rows) {
int [] newData = new int[cols * rows];
int colsToCopy = Math.min(cols, this.cols);
int rowsToCopy = Math.min(rows, this.rows);
for (int i = 0; i < rowsToCopy; ++i) {
int oldRowStart = getIndex(0, i, this.cols);
int newRowStart = getIndex(0, i, cols);
System.arraycopy(data, oldRowStart, newData, newRowStart,
colsToCopy
);
}
data = newData;
}
. . .
}
公共类IntMatrix{
私有int行;
私人公司;
私有int[]数据;
/**
*分配一个具有指定初始维度的矩阵。
*@param cols矩阵的列(水平或x)维度
*@param rows矩阵的行(垂直或y)维度
*/
公共整数矩阵(整数列,整数行){
this.rows=行;
this.cols=cols;
数据=新整数[列*行];
}
/**
*为指定的行和列计算索引
*具有指定宽度的矩阵。它使用行主排序
*矩阵元素的定义。
*
*请注意,这是一个静态方法,因此可以独立使用
*任何特定数据实例的。
*@param col所需元素的列索引
*@param row所需元素的行索引
*@param width矩阵的宽度
*/
私有静态int-getIndex(int-col、int-row、int-width){
返回行*宽度+列;
}
公共整数获取(整数列,整数行){
返回数据[getIndex(列、行、列)];
}
公共无效集(整数列、整数行、整数值){
数据[getIndex(列、行、列)]=值;
}
/**
*调整矩阵大小。放置当前矩阵中的值
*在新矩阵的左上角。在每个维度中,如果
*新大小小于当前大小,则数据为
*截断;如果新大小为i