Java 创建链表数组

Java 创建链表数组,java,arrays,linked-list,Java,Arrays,Linked List,我的程序的目的是获取一个单词列表,并以升序将每个单词存储在字母引用下的数组中。例如,A-Z单词的数组apple,链接列表下的ape由0引用,Z下的Zebra由25引用。但是当我使用标准first=newnode(word)时,我没有添加任何内容。我彻底迷路了 import java.util.LinkedList; public class ArrayLinkedList { /** The Node class is used to implement the

我的程序的目的是获取一个单词列表,并以升序将每个单词存储在字母引用下的数组中。例如,A-Z单词的数组apple,链接列表下的ape由0引用,Z下的Zebra由25引用。但是当我使用标准first=newnode(word)时,我没有添加任何内容。我彻底迷路了

import java.util.LinkedList;
public class ArrayLinkedList 
{    
   /**
    The Node class is used to implement the 
    linked list.
   */

   private class Node
   {
      String value;
      Node next;

      /**
       * Constructor
       * @param val The element to store in the node
       * @param n The reference to the successor node
       */
      Node(String val, Node n)
      {
         value = val; 
         next = n;
      }
      Node(String val)
      {
          this(val, null);
      }
    }     

    private final int MAX = 26; // Number of nodes for letters
    private Node first;         // List head
    private Node last;          // Last element in the list
    private LinkedList[] alpha; // Linked list of letter references

    /**
    * Constructor to construct empty array list
    */

    public ArrayLinkedList()
    {
        first = null;
        last = null;
        alpha = new LinkedList[MAX];

        for (int i = 0; i < MAX; i++)
        {
            alpha[i] = new LinkedList();
        }
    }

    /**
     * arrayIsEmpty method
     * To check if a specified element is empty
     */
    public boolean arrayIsEmpty(int index)
    {
        return (alpha[index].size() == 0);  
    }

    /**
     * The size method returns the length of the list
     * @return The number of elements in the list
     */
     public int size() 
     {
          int count = 0;
          Node p = first;
          while (p != null)
          {
              // There is an element at p
              count++;
              p = p.next;
          }
          return count;
     }    

    /**
     * add method
     * Adds the word to the first position in the linked list
     */
    public void add(String e)
    {
        String word = e.toLowerCase();  // Put String to lowercase
        char c = word.charAt(0);        // to get first letter of string
        int number = c - 'a';           // Index value of letter

        // Find position of word and add it to list
        if (arrayIsEmpty(number)) 
        {
            first = new Node(word);
            first = last;
        }
        else
        {
            first = sort(first, word, number);
        }     
    }

    /**
     * nodeSort method
     * To sort lists
     */
    private Node sort(Node node, String value, int number) {
        if (node == null) // End of list
        { 
            return getNode(value, number);
        }
        int comparison = node.value.compareTo(value);
        if (comparison >= 0)  // Or > 0 for stable sort.
        {
            Node newNode = getNode(value, number); // Insert in front.
            newNode.next = node;
            return newNode;
        }
        node.next = sort(node.next, value, number); // Insert in the rest.
        return node;
}

    private Node getNode(String value, int number) 
    {
        return first.next;
    }
    /**
     * get method
     * to get each word value from the linked list and return it
     * @return value
     */

    public LinkedList get(int index)
    {
        return alpha[index];
    }

    public String toString()
    {
        StringBuilder sBuilder = new StringBuilder();

        sBuilder.append("Word and occurrence in ascending order\n\n");

        Node p = first;

        while (p != null)
        {
            sBuilder.append(p.value + "\n");            
            p = p.next;
        }
        return sBuilder.toString();     
    }
}
import java.util.LinkedList;
公共类ArrayLink列表
{    
/**
Node类用于实现
链表。
*/
私有类节点
{
字符串值;
节点下一步;
/**
*建造师
*@param val要存储在节点中的元素
*@param n对后续节点的引用
*/
节点(字符串val,节点n)
{
值=val;
next=n;
}
节点(字符串val)
{
这个(val,null);
}
}     
private final int MAX=26;//字母的节点数
私有节点优先;//列表头
private Node last;//列表中的最后一个元素
私有链接列表[]alpha;//字母引用的链接列表
/**
*构造函数来构造空数组列表
*/
公共阵列链接列表()
{
第一个=空;
last=null;
alpha=新链接列表[MAX];
对于(int i=0;i=0)//或>0表示稳定排序。
{
Node newNode=getNode(值,编号);//在前面插入。
newNode.next=节点;
返回newNode;
}
node.next=排序(node.next,value,number);//插入其余部分。
返回节点;
}
私有节点getNode(字符串值,整数)
{
先返回,然后返回;
}
/**
*获取方法
*从链表中获取每个单词的值并将其返回
*@返回值
*/
公共链接列表get(整型索引)
{
返回alpha[索引];
}
公共字符串toString()
{
StringBuilder sBuilder=新StringBuilder();
sBuilder.append(“单词和出现以升序排列\n\n”);
节点p=第一;
while(p!=null)
{
sBuilder.append(p.value+“\n”);
p=p.next;
}
返回sBuilder.toString();
}
}

你这样做有什么原因吗。
我可以想出一个更简单的方法:使用Map将一个字符(例如“a”)映射到一个链接的单词列表。

它必须是一个数组,还是也可以使用其他集合?你应该先尝试编译这个。。。这甚至不会编译<代码>alpha[编号]=新节点(单词,第一个)alpha[number]是链接列表,而不是节点。那项任务是不可能的。编译器应该告诉您这一点。另外,您有一个
first
和一个
last
变量,但您有26个链表。在add方法中,您将第一个和最后一个变量视为当前正在编辑的链表(alpha[index])的属性。这似乎有点可疑。如果是这种情况,则每个链表可能都有一个
first
和一个
last
。您的代码几乎没有设计问题。我的一个建议是,不要将节点类设置为私有,将其设置为公共,并在
ArrayLinkedList
类中设置节点的私有列表。我认为您只是将两种策略混淆了。。。或者为每个字母使用java.util.LinkedList。然后,您不需要自己的节点类(在真实的word环境中,这可能是更好的方法)。或者,您可以创建自己的节点类,并自己处理列表节点的实现(如果这是一个学校项目,您应该在其中这样做)。现在,感觉你在做一些介于两者之间的事情。我如何为每个链表指定第一个和最后一个?不幸的是,我的作业概述了如何使用链表数组按升序存储英语单词。但是用数字a=0,b=1,z=25来引用字母a-z。