【发布时间】:2012-07-29 19:01:15
【问题描述】:
我创建了自己的自定义链接列表(代码如下)。现在,我不明白如何创建像LinkedList[] l = new LinkedList[10] 这样的链接列表的数组。谁能帮帮我。
class Node {
public int data;
public Node pointer;
}
class LinkedList {
Node first;
int count = 0;
public void addToEnd(int data){
if(first == null){
Node node = new Node();
node.data = data;
node.pointer = null;
first = node;
count = 1;
return;
}
Node next = first;
while(next.pointer != null){
next = (Node)next.pointer;
}
Node newNode = new Node();
newNode.data = data;
newNode.pointer = null;
next.pointer = newNode;
count++;
}
public Node getFirst(){
return first;
}
public Node getLast(){
Node next = first;
while(next.pointer != null)
next = next.pointer;
return next;
}
public int[] get(){
if(count != 0){
int arr[] = new int [count] ;
Node next = first;
int i = 0;
arr[0]= next.data;
while(next.pointer != null){
next = next.pointer;
i++;
arr[i] = next.data;
}
i++;
return arr ;
}
return null ;
}
public int count(){
return count;
}
}
【问题讨论】:
-
您在尝试创建列表时遇到什么错误?
-
new LinkedList[10]有什么问题? -
@DougRamsey, LinkedList[] l = new LinkedList[2]; for(int i=0 ; i
-
可能是 NullPointerException。我会写一个答案来解释原因。或者更确切地说,如果有人不先这样做,我会这样做。
-
@Jeffrey, LinkedList[] l = new LinkedList[2]; for(int i=0 ; i
标签: java