Java - 自定义链接列表问题
问题描述:
我已经创建了自己的自定义链接列表(代码如下)。现在,我无法理解如何创建像LinkedList[] l = new LinkedList[10]
这样的链接列表的数组。谁能帮我。Java - 自定义链接列表问题
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;
}
}
答
我要去猜测,你的问题就是,当你创建对象的数组,像
LinkedList[] lists = new LinkedList[10];
你会得到一个数组充满null
秒;您需要创建对象以存储在阵列中:
for (int i=0; i<lists.length; ++i)
lists[i] = new LinkedList();
+0
工作正常。谢谢。 – Arpssss 2012-07-31 22:22:42
+2
对于Object数组,我看到的方式是声明数组就像拿一堆盒子,然后说:“嘿,我要将存储到这些数组中。他们呢,但是当我这样做时,他们会成为 s“ – 2012-07-31 22:23:50
当您尝试创建列表时会出现什么错误? – 2012-07-31 22:17:09
'新LinkedList [10]'有什么问题? – Jeffrey 2012-07-31 22:17:16
@DougRamsey,LinkedList [] l = new LinkedList [2]; (int i = 0; i Arpssss 2012-07-31 22:18:32