在 2D 链表中添加一条线
Adding a line into 2D LinkedList
从 2D T[][]x 我需要将它的每个值复制到 2D LinkedList。我的代码在这一行中给出了错误:
myBoard.addAll((Iterable<AdditiveList<T>>) newLine);
通过投掷 NullPointerException
我的 LinkedList class 有方法 addAll(Iterable<T> c)
。如何将整行添加到二维列表中?
Class Tester
public class Tester {
public static void main(String[] args){
Integer mat [][] = {
{ 1, 2, 3, 0},
{ 0, 0, 0, 0},
{ 4, 0, 5, 6},
};
Integer fill = new Integer(0);
SparseBoard<Integer> myBoard = new SparseBoard<Integer>(mat, fill);
String s = myBoard.createBoard();
System.out.println(s);
}
}
Class Board
public class Board<T> {
private LinkedList<LinkedList<T>> myBoard = new LinkedList<LinkedList<T>>(); //Initialized inside constructors
public Board(T[][] x, T fillElem){
LinkedList<T> newLine;
for(int i = 0; i < x.length; i++){
newLine = new LinkedList<T>();
//Iterator<T> iter =
myBoard.addAll((Iterable<AdditiveList<T>>) newLine);// <<<<-------- getting error here
for(int j = 0; j < x[i].length; j++){
newLine.add(j, x[i][j]);
}
}
}
Class LinkedList
public class LinkedList<T> implements Iterable<T>{
// Doubly-linked list node for use internally
public static class Node<T> {
public T data;
public Node<T> prev, next;
public Node(T d, Node<T> p, Node<T> n) {
this.data = d;
this.prev = p;
this.next = n;
}
public Node(T d){
this.data = d;
}
}
.......................................
.......................................
public void add( int idx, T x ){
Node<T> p = getNode( idx, 0, size( ) );
Node<T> newNode = new Node<T>( x, p.prev, p );
newNode.prev.next = newNode;
p.prev = newNode;
theSize++;
}
public boolean addAll(Iterable<T> c){
boolean added = false;
for(T thing : c){
added |= this.add(thing);
}
return added;
}
..............................
.............................
}
您需要初始化 myBoard
,因为您正在声明它,但没有初始化任何东西,所以它的值为 null。
你可以用
初始化它
private LinkedList<LinkedList<T>> myBoard = new LinkedList<LinkedList<T>>();
或者,如果使用 java 7 或更高版本,您可以使用菱形运算符来简化它
private LinkedList<LinkedList<T>> myBoard = new LinkedList<>();
从 2D T[][]x 我需要将它的每个值复制到 2D LinkedList。我的代码在这一行中给出了错误:
myBoard.addAll((Iterable<AdditiveList<T>>) newLine);
通过投掷 NullPointerException
我的 LinkedList class 有方法 addAll(Iterable<T> c)
。如何将整行添加到二维列表中?
Class Tester
public class Tester {
public static void main(String[] args){
Integer mat [][] = {
{ 1, 2, 3, 0},
{ 0, 0, 0, 0},
{ 4, 0, 5, 6},
};
Integer fill = new Integer(0);
SparseBoard<Integer> myBoard = new SparseBoard<Integer>(mat, fill);
String s = myBoard.createBoard();
System.out.println(s);
}
}
Class Board
public class Board<T> {
private LinkedList<LinkedList<T>> myBoard = new LinkedList<LinkedList<T>>(); //Initialized inside constructors
public Board(T[][] x, T fillElem){
LinkedList<T> newLine;
for(int i = 0; i < x.length; i++){
newLine = new LinkedList<T>();
//Iterator<T> iter =
myBoard.addAll((Iterable<AdditiveList<T>>) newLine);// <<<<-------- getting error here
for(int j = 0; j < x[i].length; j++){
newLine.add(j, x[i][j]);
}
}
}
Class LinkedList
public class LinkedList<T> implements Iterable<T>{
// Doubly-linked list node for use internally
public static class Node<T> {
public T data;
public Node<T> prev, next;
public Node(T d, Node<T> p, Node<T> n) {
this.data = d;
this.prev = p;
this.next = n;
}
public Node(T d){
this.data = d;
}
}
.......................................
.......................................
public void add( int idx, T x ){
Node<T> p = getNode( idx, 0, size( ) );
Node<T> newNode = new Node<T>( x, p.prev, p );
newNode.prev.next = newNode;
p.prev = newNode;
theSize++;
}
public boolean addAll(Iterable<T> c){
boolean added = false;
for(T thing : c){
added |= this.add(thing);
}
return added;
}
..............................
.............................
}
您需要初始化 myBoard
,因为您正在声明它,但没有初始化任何东西,所以它的值为 null。
你可以用
初始化它private LinkedList<LinkedList<T>> myBoard = new LinkedList<LinkedList<T>>();
或者,如果使用 java 7 或更高版本,您可以使用菱形运算符来简化它
private LinkedList<LinkedList<T>> myBoard = new LinkedList<>();