[英]How to implement my own LinkedList<LinkedList> data structure in Java?
I am working on something which would make it easier if several of the objects I use would be written by me (instead of using Java's libraries). 我正在做一些事情,如果我使用的多个对象是由我自己编写的(而不是使用Java的库),这将使工作变得更加容易。 I am currently stuck at implementing an
ArrayList<ArrayList>
object (which we can call MyHashTable
) using my LinkedList
class and my Node
class. 我目前坚持使用
LinkedList
类和Node
类实现ArrayList<ArrayList>
对象(我们可以将其称为MyHashTable
)。 Here is my correctly implemented LinkedList
class so far: 到目前为止,这是我正确实现的
LinkedList
类:
public class LinkedList{
Node start;
int length;
public LinkedList(){
start = new Node();
length= 0;
}
public void addNode(Node node){
Node s= start;
while (s.next != null){
s= s.next;
}
s.next= node;
length++;
}
public int getLength(){
return length;
}
public boolean findNode(Node node){
Node s= start;
while(s.next != null){
if (s == node){
return true;
}
s.next= node;
}
return false;
}
}
So I am having trouble modifying this class to accept Java generics (so I could make a Linked List of Linked Lists instead of simply Linked List of Nodes). 因此,我在修改此类以接受Java泛型时遇到了麻烦(因此我可以制作一个“链表的链表”,而不是简单的“节点的链表”)。 Let me know if I should provide how my
Node
class looks. 让我知道是否应提供
Node
类的外观。
A generic LinkedList just substitutes the type of the value. 通用LinkedList只是替换值的类型。 You don't show your Node class so I don't understand how you're using it.
您没有显示Node类,所以我不明白您如何使用它。 Here is a generic linked list:
这是一个通用的链表:
class LinkedList<E> {
static class Node<E> {
E value;
Node<E> next;
Node(E value) {
this.value = value;
}
}
Node<E> head = new Node<E>(null);
Node<E> tail = head;
int size;
void add(E value) {
tail = tail.next = new Node<E>(value);
size++;
}
E get(int index) {
if(index < 0 || size <= index)
throw new OutOfBoundsException(index);
Node<E> node = head.next;
while(index > 0) {
node = node.next;
index--;
}
return node.value;
}
}
But I'm not sure what you mean by make that in to an ArrayList. 但是我不确定将其放入ArrayList是什么意思。 An ArrayList is completely different, it is an array that resizes itself automatically:
ArrayList完全不同,它是一个自动调整自身大小的数组:
class ArrayList<E> {
Object[] array = new Object[10];
int size;
void add(E value) {
if(size >= array.length) {
array = Arrays.copyOf(array, (int)(size * 3L / 2L));
}
array[size++] = value;
}
E get(int index) {
return (E)array[index];
}
}
And while I suppose you could hack together a hash table by using a multidimensional array, I don't recommend it. 虽然我想您可以使用多维数组将哈希表合并在一起,但我不建议这样做。 You cannot just go and instantiate an array with 2^32 elements so that means you have to manage intersections.
您不能仅使用2 ^ 32个元素实例化数组,因此这意味着您必须管理交叉点。 I don't see how an ArrayList<ArrayList> could ever be a working hash table.
我看不到ArrayList <ArrayList>怎么可能是工作哈希表。 Here is a simple hash table implementation similar to the Java one.
这是一个类似于Java的简单哈希表实现。 The table is an array of linked lists.
该表是链接列表的数组。
class HashTable<K, V> {
static class Entry<K, V> {
K key;
V value;
Entry<K, V> next;
Entry(K key, V value) {
this.key = key;
this.value = value;
}
}
Entry[] table = new Entry[8];
int size;
void put(K key, V value) {
Entry<K, V> entry = table[indexFor(key)];
while(entry != null) {
if(entry.key.equals(key)) {
entry.value = value;
return;
}
entry = entry.next;
}
resizeIfNeeded();
addEntry(new Entry<K, V>(key, value));
size++;
}
void addEntry(Entry<K, V> newEntry) {
int index = indexFor(newEntry.key);
Entry<K, V> entry = table[index];
if(entry == null) {
table[index] = newEntry;
} else {
while(entry.next != null)
entry = entry.next;
entry.next = newEntry;
}
}
void resizeIfNeeded() {
if(size < table.length)
return;
Entry[] old = table;
table = new Entry[old.length << 1];
for(Entry<K, V> entry : old) {
while(entry != null) {
addEntry(entry);
Entry<K, V> next = entry.next;
entry.next = null;
entry = next;
}
}
}
V get(K key) {
Entry<K, V> entry = table[indexFor(key)];
while(entry != null) {
if(entry.key.equals(key))
return entry.value;
entry = entry.next;
}
return null;
}
int indexFor(K key) {
return key.hashCode() & table.length - 1;
}
}
As I mentioned in my comment, this sounds like an XY problem . 正如我在评论中提到的那样,这听起来像是XY问题 。 I don't see how this is easier than using the Java data structures.
我没有看到这比使用Java数据结构更容易。 Perhaps you have a different question to ask.
也许您有其他问题要问。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.