简体   繁体   English

如何在不使用arraylist的情况下调整多维数组的大小(java)

[英]How to resize a multidimensional array without using an arraylist (java)

I need to reset the size of an array from w[i][j][q][k] to w[i+2][j+2][q][k]. 我需要将数组的大小从w [i] [j] [q] [k]重置为w [i + 2] [j + 2] [q] [k]。 I don't want to use an array of lists as I would have to change large parts of my program. 我不想使用列表数组,因为我必须更改程序的大部分内容。 I read in some threads that it is possible to create a new array of the desired size, and copy the contents from the original array to the new array using java.lang.System.arraycopy(...). 我在一些线程中读到,可以创建所需大小的新数组,然后使用java.lang.System.arraycopy(...)将内容从原始数组复制到新数组。

I tried this as follows, but it does not work with my approach: 我尝试了如下操作,但不适用于我的方法:

 int [][][][] w = new int [18][18][[Main.V+1][Main.k]; 

 (...)

 int[][][][] wNew = new int[20][20][Main.V+1][Main.k]; 

 for(int i=0; i<wNew.length; i++){
        for(int j=0; j<wNew[0].length; j++){
            for(int q=0; q<wNew[0][0].length; q++){
                for(int k=0; k<wNew[0][0][0].length; k++){
                    System.arraycopy(w, 0, wNew, 0, 18);
                }
            }
        }
    }

w = wNew;

(...)

when manipulating the array at the added positions, a java.lang.ArrayIndexOutOfBoundsException: 18 occurs (example below: 在添加位置处操作数组时,发生java.lang.ArrayIndexOutOfBoundsException:18(以下示例:

w[0][19][1][0] = 1; (this line now causes an error)

System.arraycopy(w, 0, wNew, 0, 20); System.arraycopy(w,0,wNew,0,20);

this 20 is the number of array element to copy. 这20是要复制的数组元素的数量。 You've put the new size, use the old one. 您已放入新尺寸,使用旧尺寸。 In your example it's 18. 在您的示例中为18。

Secondly your for loops are based on your new size. 其次,您的for循环基于您的新大小。 This is backward, you have to read your original array and insert into the new array, so you have to iterate on the original array size(for resizing up, of course to make it smaller it would be the other way around). 这是向后的,您必须读取原始数组并将其插入新数组,因此必须迭代原始数组的大小(要调整大小,当然要减小它的大小,反之亦然)。

But more importantly you don't have to iterate on all the dimentions. 但更重要的是,您不必遍历所有尺寸。 I'll past you some code you'll can execute to see for yourself. 我将为您传递一些代码,您可以执行这些代码以亲自查看。

import java.util.Arrays; 导入java.util.Arrays; import java.util.Random; 导入java.util.Random;

public class Test {

    public static final int SIZE_DIM1 = 2;
    public static final int SIZE_DIM2 = 2;
    public static final int SIZE_DIM3 = 5;
    public static final int SIZE_DIM4 = 5;
    private static final int INCREMENT = 2;

    public static void main(String[] args) {
        int[][][][] w = new int[SIZE_DIM1][SIZE_DIM2][SIZE_DIM3][SIZE_DIM4];

        randomFill(w);

        display(w);

        int[][][][] wNew = new int[SIZE_DIM1 + INCREMENT][SIZE_DIM2 + INCREMENT][SIZE_DIM3][SIZE_DIM4];

        for (int i = 0; i < w.length; i++) {
            for (int j = 0; j < w[i].length; j++) {
                System.arraycopy(w[i][j], 0, wNew[i][j], 0, w[i][j].length);
            }
        }

        display(wNew);

        w = wNew;

        w[0][3][4][4] = 1;

    }

    public static void randomFill(int[][][][] w) {
        Random random = new Random();
        for (int[][][] w2 : w) {
            for (int[][] w3 : w2) {
                for (int[] w4 : w3) {
                    for (int i = 0; i < w4.length; i++) {
                        w4[i] = random.nextInt();
                    }
                }
            }
        }
    }

    public static void display(int[][][][] w) {
        System.out.println("Printing---------------------------------------------------------------------------------");


        System.out.print("[\n");
        for (int[][][] w2 : w) {
            System.out.print("\t[\n");
            for (int[][] w3 : w2) {
                System.out.print("\t\t[\n");
                for (int[] w4 : w3) {
                    System.out.print("\t\t\t[");
                    for (int element : w4) {
                        System.out.print(element + " ");
                    }
                    System.out.print("]\n");
                }
                System.out.print("\t\t]\n");
            }
            System.out.print("\t]\n");
        }
        System.out.print("]\n");

    }

}

As you can see you don't have to iterate on all the sub arrays. 如您所见,您不必遍历所有子数组。 just on the ones that have their sizes changing. 只是大小有所变化的那些。

Execute it and it will be obvious. 执行它,这将是显而易见的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM