代码之家  ›  专栏  ›  技术社区  ›  Thomas Linssen

调整二维数组?

  •  0
  • Thomas Linssen  · 技术社区  · 7 年前

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

        char Grid[][] = {
        {'#','#','#'}
        {'#','#','#'}
        {'#','#','#'}
        }
    

    并将其显示为:

            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();
        }
    

    我希望能够简单地调整数组中的元素,比如添加和删除元素。由于基本java(出于某种原因)似乎没有任何预定义的函数来实现这一点,所以我尝试使用common langs中的ArrayUtils类。我在文档中找到了几个方法,包括“添加”、“插入”和“删除”,并尝试了以下方法:

            ArrayUtils.insert(Scene, Grid, 2); //(With "Scene" being the class name)
    

    但正如所料,它没有起作用。

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

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

    2 回复  |  直到 7 年前
        1
  •  3
  •   Nahuel Fouilleul    7 年前

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

        2
  •  0
  •   dbl    7 年前

    一旦在java中创建了一个具有固定长度的数组,那么在您的情况下,在使用数组Inicializer之后,您将得到一个 长度为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.
    }
    

    要从数组中删除元素,必须创建一个大小为[长度-1]的新数组,跳过要删除的元素并添加所有其他元素。然后将新的分配给矩阵中的索引。简单代码:

    private static void remove(char[][] grid, int row, int column) {
        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) {
            throw new ArrayIndexOutOfBoundsException("No such column with index " + column + " at row " + row + " inside the matrix."); // you are trying to insert an element out of the array's bounds.
        }
    
        boolean flag = false; //indicates that the element has been removed.
        char[] temp = new char[grid[row].length - 1];
    
        for (int i = 0; i < temp.length; i++) {
            if (i == column) {
                flag = true;
            }
            temp[i] = grid[row][i + (flag ? 1 : 0)];
        }
    
        grid[row] = temp; //assign the new value of the whole row to it's placeholder.
    }
    

    如果我定义一个名为 print(char[][] grid) 然后输入用于打印的代码,我就可以进行以下测试:

    add(Grid, 2, 3, '$'); //Add an element.
    print(Grid);
    System.out.println();
    remove(Grid, 2, 0); // Remove an element.
    print(Grid);
    System.out.println();
    Grid[0][0] = '%'; // Change an element's value.
    print(Grid);
    

    输出如下:

    ###
    ###
    ###$
    
    ###
    ###
    ##$
    
    %##
    ###
    ##$