Java 如何更改静态数组的大小?

Java 如何更改静态数组的大小?,java,arrays,static,size,Java,Arrays,Static,Size,我需要更改静态数组的大小 我有: static int ca [][] = { {45}, {0}, {45}, {0} }; 并尝试: ca[][] = new int[5][5]; 不能更改数组的大小。如果您想要相同的功能但能够更改大小,请使用arraylist。对于int的数组列表: ArrayList<Integer> myList =

我需要更改静态数组的大小

我有:

static int ca [][] = { {45},
                       {0},
                       {45},
                       {0} };
并尝试:

ca[][] = new int[5][5];

不能更改数组的大小。如果您想要相同的功能但能够更改大小,请使用arraylist。对于int的数组列表:

ArrayList<Integer> myList = new ArrayList<Integer>();
ArrayList myList=new ArrayList();

你不可能很容易做到。如果需要调整数组的大小,请使用ArrayList。下面是一个例子:

ArrayList list = new ArrayList<Object>();
list.add(obj);
Object obj2 = list.get(index);
ArrayList list=new ArrayList();
列表。添加(obj);
对象obj2=list.get(索引);

“对象”可以替换为任何类或数据类型。查看文档了解更多信息

您可以创建类似以下内容的帮助器函数:

static int[][] ca ={ {45}, 
                      {0},
                     {45},
                      {0} };

public static void main(String[] args) {
    ca = increaseSize(ca, 5, 5);
}

public static int[][] increaseSize(int[][] array, int rowSize, int colSize) {
    if (array.length <= rowSize && array[0].length <= colSize) {
        int temp[][] = new int[rowSize][colSize];
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[0].length; j++) {
                temp[i][j] = array[i][j];
            }
        }
        return temp;
    }
    return null;
}
static int[][]ca={{45},
{0},
{45},
{0} };
公共静态void main(字符串[]args){
ca=增量大小(ca,5,5);
}
公共静态int[][]递增大小(int[][]数组、int行大小、int列大小){

如果(array.length我建议使用ArrayList或Vector。与ArrayList不同,添加最后一个元素时,Vector的大小不会增加一倍。

你不会。数组的大小是固定的,在创建数组时设置。请避免糟糕的格式。使用适当的缩进并正确表达你的问题。你尝试过了,结果如何你没想到会这样?想想看。