如何从2d数组到双链表的值?我知道如何用数组列表,但我不知道如何实现它与双重链表。我怎么能复制一切从2d数组到LinkedList?我需要LinkedList中的LinkedList吗?
如果我有
int[][] myArray = {{1,2,3}
{4,5,6},
{7,8,9}};
那么LinkedList应该看起来完全一样,即:
[[1,2,3]
[4,5,6],
[7,8,9];
public void copyFromArray(int[][] myArray){
}
public class Node<Integer> {
public Integer data;
public Node<Integer> prev, next;
public Node( Integer d, Node<Integer> p, Node<Integer> n ){
this.data = d;
this.prev = new Node();
this.next = new Node();
}
}
是的,你需要一个LinkedLists的LinkedList:
LinkedList<LinkedList<Integer>> list = new LinkedList<>();
int[][] myArray = { {1, 2, 3}, {4,5,6},{7,8,9} };
for (int i = 0; myArray.length >= i; i++) {
LinkedList<Integer> auxList = new LinkedList<>();
for (int j = 0; myArray[i].length >= j; j++) {
auxList.add(myArray[i][j]);
}
list.add(auxList);
}