如何将 ArrayList<ArrayList<String>> 转换为 Java 中的 List<List<String>>?
How to convert an ArrayList<ArrayList<String>> to List<List<String>> in Java?
我正在研究 LeetCode 上的 N-Queens 问题,它规定 main 方法的 return 类型是 List<List<String>>
。我认为如果我创建一个 List<List<String>>
的全局变量,然后在 main 中将其实例化为 ArrayList<ArrayList<String>>
,我将能够 return 作为正确的类型,但是当我尝试实例化它会给出错误:"Line 12: error: incompatible types: ArrayList<ArrayList<StringBuilder>>
cannot be converted to ArrayList<ArrayList<String>>
"。
我的印象是,因为 ArrayList 实现了 List,所以您可以 return ArrayList 的实例作为 List,但是当我将全局变量设为 ArrayList 时,它会出现以下错误:"Line 15: error: incompatible types: ArrayList<ArrayList<String>>
cannot be converted to List<List<String>>
"。打印出被认为有效的电路板我知道我得到了正确的组合,但最后这个细节让我很沮丧,我非常感谢任何解释为什么我会出现这些错误。谢谢!
class Solution {
ArrayList<Integer> colsUsed;
ArrayList<Integer> leftDiagsUsed;
ArrayList<Integer> rightDiagsUsed;
ArrayList<ArrayList<String>> solutions;
public List<List<String>> solveNQueens(int n) {
colsUsed = new ArrayList<Integer>();
leftDiagsUsed = new ArrayList<Integer>();
rightDiagsUsed = new ArrayList<Integer>();
ArrayList<StringBuilder> board = createBoard(n);
solutions = new ArrayList<ArrayList<String>>();
return solutions;
}
public void iterateBoards(ArrayList<StringBuilder> board, int row, int n){
if (row >= n){
printBoard(board);
addBoard(board);
return;
}
for (int col = 0; col < n; col++){
if (!colsUsed.contains(col) && !leftDiagsUsed.contains(row - col) && !rightDiagsUsed.contains(row + col)){
colsUsed.add(col);
leftDiagsUsed.add(row - col);
rightDiagsUsed.add(row + col);
board.get(row).setCharAt(col, 'Q');
iterateBoards(board, row + 1, n);
colsUsed.remove(new Integer(col));
leftDiagsUsed.remove(new Integer(row - col));
rightDiagsUsed.remove(new Integer(row + col));
board.get(row).setCharAt(col, '.');
}
}
}
public ArrayList<StringBuilder> createBoard(int n){
StringBuilder row = new StringBuilder();
ArrayList<StringBuilder> board = new ArrayList<StringBuilder>(n);
for (int col = 0; col < n; col++){
row.append(".");
}
for (int currRow = 0; currRow < n; currRow++){
board.add(new StringBuilder(row.toString()));
}
return board;
}
public void addBoard(ArrayList<StringBuilder> board){
List<String> newBoard = new ArrayList<String>();
for (int i = 0; i < board.size(); i++){
newBoard.add(board.get(i).toString());
}
solutions.add(newBoard);
}
public void printBoard(ArrayList<StringBuilder> board){
for (int i = 0; i < board.size(); i++){
StringBuilder curr = board.get(i);
System.out.println(curr.toString());
}
System.out.println();
}
}
更改所有变量 声明 以仅使用 List
接口。最好在声明中使用尽可能广泛的类型 - 对于 java.util 集合,这几乎总是接口。
您选择使用 ArrayList(而不是 LinkedList 等)是一个实现细节,不会影响您的其余代码。
class Solution {
List<Integer> colsUsed;
List<Integer> leftDiagsUsed;
List<Integer> rightDiagsUsed;
List<List<String>> solutions;
public List<List<String>> solveNQueens(int n) {
// [...]
solutions = new ArrayList<List<String>>();
我正在研究 LeetCode 上的 N-Queens 问题,它规定 main 方法的 return 类型是 List<List<String>>
。我认为如果我创建一个 List<List<String>>
的全局变量,然后在 main 中将其实例化为 ArrayList<ArrayList<String>>
,我将能够 return 作为正确的类型,但是当我尝试实例化它会给出错误:"Line 12: error: incompatible types: ArrayList<ArrayList<StringBuilder>>
cannot be converted to ArrayList<ArrayList<String>>
"。
我的印象是,因为 ArrayList 实现了 List,所以您可以 return ArrayList 的实例作为 List,但是当我将全局变量设为 ArrayList 时,它会出现以下错误:"Line 15: error: incompatible types: ArrayList<ArrayList<String>>
cannot be converted to List<List<String>>
"。打印出被认为有效的电路板我知道我得到了正确的组合,但最后这个细节让我很沮丧,我非常感谢任何解释为什么我会出现这些错误。谢谢!
class Solution {
ArrayList<Integer> colsUsed;
ArrayList<Integer> leftDiagsUsed;
ArrayList<Integer> rightDiagsUsed;
ArrayList<ArrayList<String>> solutions;
public List<List<String>> solveNQueens(int n) {
colsUsed = new ArrayList<Integer>();
leftDiagsUsed = new ArrayList<Integer>();
rightDiagsUsed = new ArrayList<Integer>();
ArrayList<StringBuilder> board = createBoard(n);
solutions = new ArrayList<ArrayList<String>>();
return solutions;
}
public void iterateBoards(ArrayList<StringBuilder> board, int row, int n){
if (row >= n){
printBoard(board);
addBoard(board);
return;
}
for (int col = 0; col < n; col++){
if (!colsUsed.contains(col) && !leftDiagsUsed.contains(row - col) && !rightDiagsUsed.contains(row + col)){
colsUsed.add(col);
leftDiagsUsed.add(row - col);
rightDiagsUsed.add(row + col);
board.get(row).setCharAt(col, 'Q');
iterateBoards(board, row + 1, n);
colsUsed.remove(new Integer(col));
leftDiagsUsed.remove(new Integer(row - col));
rightDiagsUsed.remove(new Integer(row + col));
board.get(row).setCharAt(col, '.');
}
}
}
public ArrayList<StringBuilder> createBoard(int n){
StringBuilder row = new StringBuilder();
ArrayList<StringBuilder> board = new ArrayList<StringBuilder>(n);
for (int col = 0; col < n; col++){
row.append(".");
}
for (int currRow = 0; currRow < n; currRow++){
board.add(new StringBuilder(row.toString()));
}
return board;
}
public void addBoard(ArrayList<StringBuilder> board){
List<String> newBoard = new ArrayList<String>();
for (int i = 0; i < board.size(); i++){
newBoard.add(board.get(i).toString());
}
solutions.add(newBoard);
}
public void printBoard(ArrayList<StringBuilder> board){
for (int i = 0; i < board.size(); i++){
StringBuilder curr = board.get(i);
System.out.println(curr.toString());
}
System.out.println();
}
}
更改所有变量 声明 以仅使用 List
接口。最好在声明中使用尽可能广泛的类型 - 对于 java.util 集合,这几乎总是接口。
您选择使用 ArrayList(而不是 LinkedList 等)是一个实现细节,不会影响您的其余代码。
class Solution {
List<Integer> colsUsed;
List<Integer> leftDiagsUsed;
List<Integer> rightDiagsUsed;
List<List<String>> solutions;
public List<List<String>> solveNQueens(int n) {
// [...]
solutions = new ArrayList<List<String>>();