我做了这样的二维数组:
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(由于某种原因)似乎没有任何预定义函数来执行此操作,我尝试使用常见语言中的ArrayUtils类。我在文档中找到了几个方法,包括“添加”,“插入”和“删除”,并尝试过这样的方法:
ArrayUtils.insert(Scene, Grid, 2); //(With "Scene" being the class name)
但正如预期的那样,它没有用。
在另一个网站上,我读到了一些关于克隆数组的内容,但我不认为这是我的问题的解决方案,因为我希望能够移动一个ASCII字符,我不想创建一个每次移动它都会有新的数组。
编辑:要清楚,我希望能够更改索引值或快速删除然后将另一个放在确切的位置。
答案 0 :(得分:3)
数组是基本类型,不用于添加元素,对于这些操作ArrayList,并且取决于在任何地方是否有许多插入和删除,LinkedList可能更方便。 ArrayUtils.insert应该返回一个执行初始数组副本的新数组实例。
答案 1 :(得分:0)
一旦在java中创建了一个具有固定长度的数组,所以在您使用Array Inicializer之后,最终得到一个长度为3 的数组,其元素类型为 char数组其中每个固定长度均为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.
}
要从数组中删除元素,您必须创建一个大小为[length - 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);
输出如下:
###
###
###$
###
###
##$
%##
###
##$