在Java中创建二维链表
我用java创建了一个2d链表。我在构建它时使用for loop来生成所需的行和列,但是当我添加行时,列也会添加。所需的行数和列数不能正常工作。这是我的代码:在Java中创建二维链表,java,linked-list,Java,Linked List,我用java创建了一个2d链表。我在构建它时使用for loop来生成所需的行和列,但是当我添加行时,列也会添加。所需的行数和列数不能正常工作。这是我的代码: public class BoardClass{ public LinkedList<LinkedList<Integer>> empiBoard = new LinkedList<LinkedList<Integer>>(); public LinkedList<In
public class BoardClass{
public LinkedList<LinkedList<Integer>> empiBoard = new LinkedList<LinkedList<Integer>>();
public LinkedList<Integer> rowList = new LinkedList<Integer>();
public LinkedList<LinkedList<Integer>> createBoard (int col, int row){
for(int i=0; i<row; i++) {
for(int j=0; j<col; j++) {
rowList.add(0);
}
empiBoard.add(rowList);
}
return empiBoard;
}
公共类BoardClass{
public LinkedList empiBoard=new LinkedList();
public LinkedList rowList=新建LinkedList();
公共链接列表创建板(int列,int行){
对于(inti=0;i您需要不同类型的结构
public class BoardClass{
public List<List<Integer>> emptyBoard = null;
public List<List<Integer>> createBoard (int col, int row){
emptyBoard = new ArrayList<List<Integer>>(row);
for(int i=0; i<row; i++) {
List<Integer> rowList = new ArrayList(col)
for(int j=0; j<col; j++) {
rowList.add(0);
}
emptyBoard.add(rowList);
}
return empiBoard;
}
公共类BoardClass{
public List emptyBoard=null;
公共列表创建板(整数列,整数行){
emptyBoard=新阵列列表(行);
对于(int i=0;i)您必须在每次迭代中创建一个新的行列表
,否则每次都会添加到同一个精确的列表中。是的,在每次迭代中创建新的行列表,但是如何对该部分进行编码?