在没有 collection 的情况下为 Java 中的键值制作数据结构
Making data structure for Key value in Java without collection
我写了一个字符串类型的单排序链表。
让我告诉你我必须做什么。
有单词要存储在排序的链表中。
每个单词都有一些变体(或含义仅供参考),也需要存储在单排序链表中。
所以基本上链表中的每个单词都有变体链表。
问题是如何连接它们。
我的意思是当删除这个词时也会删除它们的变体。
如有任何帮助,我们将不胜感激。
提前谢谢你。
更新:
下面是排序的链表:
public class LinkedList {
private Node start = null;
private class Node{
private String value = null;
private Node next = null;
}
public void insert(){
// This method will loop from head and inserts in ascending order
}
}
// And other methods like delete etc...
以下是我正在尝试做的事情:
public class Demo {
public class Word {
private String stringWord;
private LinkedList variations;
}
private LinkedList Words;
注意:不允许使用任何 API 或 Collection
您可以对 LinkedList 使用 java 泛型,如下所示
public class LinkedList<T> {
private Node start = null;
private class Node {
private T value = null;
private Node next = null;
}
}
// for string type
LinkedList<String> variations = new LinkedList<String>();
// for word type
LinkedList<Word> words = new LinkedList<Word>();
由于您想要一个 "variations" 列表与主列表中的每个单词相关联,因此您需要主列表是 Word
对象的列表,而不仅仅是普通 [=12] 的列表=] 个对象。
假设您已经了解了泛型,这意味着您需要更改 LinkedList
class 以使用 "type parameter" 而不是硬编码 String
,这样value
可以是 Word
(对于主列表)或 String
(对于变体列表)。
由于您的列表需要排序,您需要类型参数为Comparable
,因此您的代码变为:
public class LinkedList<E extends Comparable<E>> {
private Node start = null;
private class Node{
private E value = null;
private Node next = null;
}
public void insert(E newValue){
// code here, e.g.
// node.getValue().compareTo(newValue)
}
}
public class Word implements Comparable<Word>{
private String stringWord;
private LinkedList<String> variations;
@Override
public int compareTo(Word that){
return this.stringWord.compareTo(that.stringWord);
}
}
// main list
LinkedList<Word> words;
我写了一个字符串类型的单排序链表。 让我告诉你我必须做什么。
有单词要存储在排序的链表中。 每个单词都有一些变体(或含义仅供参考),也需要存储在单排序链表中。
所以基本上链表中的每个单词都有变体链表。
问题是如何连接它们。 我的意思是当删除这个词时也会删除它们的变体。
如有任何帮助,我们将不胜感激。 提前谢谢你。
更新:
下面是排序的链表:
public class LinkedList {
private Node start = null;
private class Node{
private String value = null;
private Node next = null;
}
public void insert(){
// This method will loop from head and inserts in ascending order
}
}
// And other methods like delete etc...
以下是我正在尝试做的事情:
public class Demo {
public class Word {
private String stringWord;
private LinkedList variations;
}
private LinkedList Words;
注意:不允许使用任何 API 或 Collection
您可以对 LinkedList 使用 java 泛型,如下所示
public class LinkedList<T> {
private Node start = null;
private class Node {
private T value = null;
private Node next = null;
}
}
// for string type
LinkedList<String> variations = new LinkedList<String>();
// for word type
LinkedList<Word> words = new LinkedList<Word>();
由于您想要一个 "variations" 列表与主列表中的每个单词相关联,因此您需要主列表是 Word
对象的列表,而不仅仅是普通 [=12] 的列表=] 个对象。
假设您已经了解了泛型,这意味着您需要更改 LinkedList
class 以使用 "type parameter" 而不是硬编码 String
,这样value
可以是 Word
(对于主列表)或 String
(对于变体列表)。
由于您的列表需要排序,您需要类型参数为Comparable
,因此您的代码变为:
public class LinkedList<E extends Comparable<E>> {
private Node start = null;
private class Node{
private E value = null;
private Node next = null;
}
public void insert(E newValue){
// code here, e.g.
// node.getValue().compareTo(newValue)
}
}
public class Word implements Comparable<Word>{
private String stringWord;
private LinkedList<String> variations;
@Override
public int compareTo(Word that){
return this.stringWord.compareTo(that.stringWord);
}
}
// main list
LinkedList<Word> words;