Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.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 调整二维数组';s_Java_Arrays_Multidimensional Array_Ascii - Fatal编程技术网

Java 调整二维数组';s

Java 调整二维数组';s,java,arrays,multidimensional-array,ascii,Java,Arrays,Multidimensional Array,Ascii,我制作了一个二维数组,如下所示: char Grid[][] = { {'#','#','#'} {'#','#','#'} {'#','#','#'} } ArrayUtils.insert(Scene, Grid, 2); //(With "Scene" being the class name) 并将其显示为: for (int row = 0; row < Grid.length; row++) {

我制作了一个二维数组,如下所示:

    char Grid[][] = {
    {'#','#','#'}
    {'#','#','#'}
    {'#','#','#'}
    }
        ArrayUtils.insert(Scene, Grid, 2); //(With "Scene" being the class name)
并将其显示为:

        for (int row = 0; row < Grid.length; row++) {
        for (int column = 0; column < Grid[row].length; column++) {
            System.out.print(Grid[row][column]);
        }
        System.out.println();
    }
但正如所料,它没有起作用

在另一个网站上,我读到了一些关于克隆数组的内容,但我认为这不是我问题的解决方案,因为我希望能够在ASCII字符周围移动,并且我不希望每次移动它时都创建一个新数组


编辑:为了清楚起见,我希望能够更改索引值的位置,或者快速删除,然后将另一个放置在准确的位置

数组是基本类型,不用于添加元素,对于这些操作,ArrayList和LinkedList可能更方便,具体取决于在任何位置是否有许多插入和删除。ArrayUtils.insert应返回一个新的数组实例,并复制初始数组。

数组是基本类型,不用于添加元素,对于这些操作,ArrayList和LinkedList可能更方便,具体取决于在任何位置是否有多个插入和删除。ArrayUtils.insert应该返回一个新的数组实例来复制初始数组。

一旦在java中创建了一个长度固定的数组,那么在您的情况下,在使用数组初始化器之后,您将得到一个长度为3的数组,具有字符数组类型的元素,其中每个元素的固定长度也是3。要替换给定值,只需将新值分配给适当的索引,如:

Grid[i][j] = 'new character value';
正如我已经说过的,由于大小是固定的,所以不能向其中添加新值,除非将整个数组复制到大小为(长度+1)的新数组中,并将新值放置在所需位置。简单代码表明:

private static void add(char[][] grid, int row, int column, char value) {
    if (row < 0 || row > grid.length) {
        throw new ArrayIndexOutOfBoundsException("No such row with index " + row + " inside the matrix."); // you are trying to insert an element out of the array's bounds.
    }
    if (column < 0 || column > grid[row].length) {
        /*
         * An array in Java is with fixed length so you should keep the index inside the size!
         */
        throw new ArrayIndexOutOfBoundsException("Index " + column + " does not exists in the extended array!"); // you are trying to insert an element out of the array's bounds.
    }

    boolean flag = false; //indicates that the new element has been inserted.
    char[] temp = new char[grid[row].length + 1];

    for (int i = 0; i < temp.length; i++) {
        if (i == column) {
            temp[i] = value;
            flag = true;
        } else {
            temp[i] = grid[row][i - (flag ? 1 : 0)];
        }
    }

    grid[row] = temp; //assign the new value of the whole row to it's placeholder.
}
输出如下:

###
###
###$

###
###
##$

%##
###
##$

一旦在java中创建了一个固定长度的数组,那么在您使用数组初始化器之后,您将得到一个长度为3的数组,该数组的元素类型为char array,其中每个元素的长度也固定为3。要替换给定值,只需将新值分配给适当的索引,如:

Grid[i][j] = 'new character value';
正如我已经说过的,由于大小是固定的,所以不能向其中添加新值,除非将整个数组复制到大小为(长度+1)的新数组中,并将新值放置在所需位置。简单代码表明:

private static void add(char[][] grid, int row, int column, char value) {
    if (row < 0 || row > grid.length) {
        throw new ArrayIndexOutOfBoundsException("No such row with index " + row + " inside the matrix."); // you are trying to insert an element out of the array's bounds.
    }
    if (column < 0 || column > grid[row].length) {
        /*
         * An array in Java is with fixed length so you should keep the index inside the size!
         */
        throw new ArrayIndexOutOfBoundsException("Index " + column + " does not exists in the extended array!"); // you are trying to insert an element out of the array's bounds.
    }

    boolean flag = false; //indicates that the new element has been inserted.
    char[] temp = new char[grid[row].length + 1];

    for (int i = 0; i < temp.length; i++) {
        if (i == column) {
            temp[i] = value;
            flag = true;
        } else {
            temp[i] = grid[row][i - (flag ? 1 : 0)];
        }
    }

    grid[row] = temp; //assign the new value of the whole row to it's placeholder.
}
输出如下:

###
###
###$

###
###
##$

%##
###
##$

数组不可调整大小。尝试改用列表。您希望对数组执行什么操作?您可以只是偶然获得其中的一个元素,但在分配它们之后,您不能使它们变长或变短。您说要调整数组,但不清楚要进行什么样的精确调整,也不清楚为什么ArrayUtils.insert没有达到预期效果。数组无法调整大小。尝试改用列表。您希望对数组执行什么操作?您可以只是偶然获得其中的一个元素,但在分配它们之后,您不能使它们变长或变短。您说要调整数组,但不清楚要进行哪些精确调整,也不清楚为什么ArrayUtils.insert没有达到预期效果。