Java 使用整数[]与整数[]
我试图解决以下问题:“编写一个算法,在8x8国际象棋板上打印所有排列八个皇后的方式,以便它们都不共享同一行、列或对角线(即,没有两个皇后相互攻击)。” 我很难理解作者为什么使用Integer[]而不是更常见的int[],例如在“Integer[]列”和“ArrayList results”中,这是placequeen的参数。我的假设是这是由于Java中的泛型造成的,但我不能完全确定 下面是代码片段。链接到页面底部的完整代码Java 使用整数[]与整数[],java,recursion,integer,boxing,Java,Recursion,Integer,Boxing,我试图解决以下问题:“编写一个算法,在8x8国际象棋板上打印所有排列八个皇后的方式,以便它们都不共享同一行、列或对角线(即,没有两个皇后相互攻击)。” 我很难理解作者为什么使用Integer[]而不是更常见的int[],例如在“Integer[]列”和“ArrayList results”中,这是placequeen的参数。我的假设是这是由于Java中的泛型造成的,但我不能完全确定 下面是代码片段。链接到页面底部的完整代码 public static int GRID_SIZE = 8; /*
public static int GRID_SIZE = 8;
/* Check if (row1, column1) is a valid spot for a queen by checking if there
* is a queen in the same column or diagonal. We don't need to check it for queens
* in the same row because the calling placeQueen only attempts to place one queen at
* a time. We know this row is empty.
*/
public static boolean checkValid(Integer[] columns, int row1, int column1) {
for (int row2 = 0; row2 < row1; row2++) {
int column2 = columns[row2];
/* Check if (row2, column2) invalidates (row1, column1) as a queen spot. */
/* Check if rows have a queen in the same column */
if (column1 == column2) {
return false;
}
/* Check diagonals: if the distance between the columns equals the distance
* between the rows, then they're in the same diagonal.
*/
int columnDistance = Math.abs(column2 - column1);
int rowDistance = row1 - row2; // row1 > row2, so no need to use absolute value
if (columnDistance == rowDistance) {
return false;
}
}
return true;
}
public static void placeQueens(int row, Integer[] columns, ArrayList<Integer[]> results) {
if (row == GRID_SIZE) { // Found valid placement
results.add(columns.clone());
} else {
for (int col = 0; col < GRID_SIZE; col++) {
if (checkValid(columns, row, col)) {
columns[row] = col; // Place queen
placeQueens(row + 1, columns, results);
}
}
}
}
public static int GRID\u SIZE=8;
/*检查(第1行,第1列)是否为女王的有效位置
*是同一列或对角线中的皇后。我们不需要检查皇后区
*在同一行中,因为呼叫placeQueen只尝试在同一行中放置一个queen
*一段时间。我们知道这一排是空的。
*/
公共静态布尔检查有效(整数[]列,整数行1,整数列1){
对于(int row2=0;row2row2,所以不需要使用绝对值
如果(columnDistance==行距离){
返回false;
}
}
返回true;
}
公共静态void placequeen(int行、整数[]列、ArrayList结果){
如果(行==网格大小){//找到有效位置
results.add(columns.clone());
}否则{
对于(int col=0;col
问题/代码来源:破解编码面试。链接到完整代码:您可能认为main的第一行(来自您提供的链接):
ArrayList结果=新建ArrayList();
必须使用整数,但正如注释所示,情况并非如此
ArrayList<int[]> results = new ArrayList<int[]>();
ArrayList结果=新建ArrayList();
也会起作用。在Java中,
Integer
表示一个对象,而int
是一个基本类型。Integer
类支持更多函数,可以保存null
值。此外,ArrayList
只能包含Integer
等对象
ArrayList<int[]> results = new ArrayList<int[]>();
ArrayList结果=新建ArrayList();
在上面修订的代码中,
int[]
仍然可以工作,因为它被视为一个对象。但是,作者可能正在寻求一致性,或者需要Integer
对象的额外功能。这是作者的偏好或无知。这与泛型无关。即使这些值是int,Java也会在需要时将它们自动装箱到Integer类。int是一种基本类型,其中as Integer是此基本类型int的包装类。请尝试阅读Java中的基本数据类型和自动装箱/取消装箱。这里的用法完全基于观点。除非我遗漏了一些明显的内容,否则这是一个直截了当的错误。您可以使用int[]
,代码的行为也会相同(只是这样会减少浪费)。@NathanHughes通常是这样的。在本例中,使用Integer[]
而不是int[]
没有任何优势,但有一些“一般”优势:例如默认值。尽管null
毫无意义,但它仍然比0
好。另一个优点是Integer[]
是对象类型的数组,而int[]
不能使用泛型确定为int
的数组。这就是为什么Arrays.asList(new int[]{1,2,3})
不能按预期工作的原因。但是int[]
是一个对象。@JFPicard-List a=new arrarylist();很好,你说得对,我不知道。。。我在想,即使是一个基元类型数组->基元类型。我会编辑。@Siguza那么我想唯一的原因是为了获得更多的功能?或者可能只是一个简单的错误?我已经编辑了答案,请检查它是否正确。@biziclop“我们可以假设文章中没有的代码”我们可以,或者我们按照发布的链接阅读完整的代码:P和no,开发人员只使用取消装箱/自动装箱,其他什么都不用。因此Integer
和Integer[]
在这里毫无意义。@DizzyCode请检查完整的代码,特别是clear()
方法。现在,我不是夏洛克·福尔摩斯,但我猜写这段代码的人很幸运地没有意识到int
和Integer
之间的区别。
ArrayList<int[]> results = new ArrayList<int[]>();