[英]Java - Custom Linked List Issue
I have created my own custom Linked List (code is below). 我创建了自己的自定义链接列表(下面的代码)。 Now, I can't understand how to create an array of that Linked list like
LinkedList[] l = new LinkedList[10]
. 现在,我不明白如何创建该链接列表的数组,例如
LinkedList[] l = new LinkedList[10]
。 Can anyone help me. 谁能帮我。
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;
}
}
I'm going to guess that your problem is just that when you create an array of objects, like 我会猜测,您的问题就是创建对象数组时,例如
LinkedList[] lists = new LinkedList[10];
you get an array full of null
s; 您得到一个充满
null
的数组; you need to create objects to store in the array: 您需要创建对象以存储在数组中:
for (int i=0; i<lists.length; ++i)
lists[i] = new LinkedList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.