LinkedList类。 (ReflectiveOperationException)

时间:2015-04-10 11:13:43

标签: java list

我正在学习Java SE,目前正在使用简单的链接列表(Savitch的Absolute Java的第687/1047页)。

我无法在我的演示类的main方法中实例化LinkList

LinkedList1 list = new LinkedList1();

我尝试使用断点,它表示ReflectiveOperationException。 这是代码:

public class Node1 
{  
    private String item;
    private int count;
    private Node1 link;

    public Node1()
    {
        link = null;
        item = null;
        count = 0;

    }

    public Node1(String newItem, int newCount, Node1 linkValue)
    {
        setData(newItem, newCount);
        link = linkValue;
    }

    public void setData(String newItem, int newCount)
    {
        item = newItem;
        count = newCount;
    }

    public void setLink(Node1 newLink)
    {
        link = newLink;
    }

    public String getItem()
    {
        return item;
    }

    public int getCount()
    {
        return count;
    }

    public Node1 getLink()
    {
        return link;
    }
}

这是LinkedList1类:

public class LinkedList1 
{
    private Node1 head;

    public LinkedList1()
    {
        head = null;
    }

    /**
     * Adds a node at the start of the list with the specified data.
     * The added node will be the first node in the list.
     */
    public void add(String itemName, int itemCount)
    {
        head = new Node1(itemName, itemCount, head);
    }


    /**
     * Removes the head node and returns true if the list contains at least
     * one node. Returns false if the list is empty.
     */
    public boolean deleteHeadNode()
    {

        if (head != null)
        {
            head = head.getLink();
            return true;
        }
        else
            return false;
    }

    /**
     * Returns the number of nodes in the list.
     */
    public int size()
    {
        int count = 0;
        Node1 position = head;
        while (position != null)
        {
            count++;
            head = position.getLink();
        }

        return count;
    }


    public boolean contains(String item)
    {
        return (find(item) != null);
    }


    /**
     * Finds the first node containing the target item, and returns a
     * reference to that node. If the target is not in the list, null is returned.
     */
    public Node1 find(String target)
    {
        Node1 position = head;
        String itemAtPosition;
        while(position != null)
        {
            itemAtPosition = position.getItem();
            if(itemAtPosition.equals(target))
            {
                return position;

            }
            position = position.getLink();
        }

        return null; //target was not found
    }


    public void outputList()
    {

        Node1 position = head;
        while (position != null)
        {
            System.out.println(position.getItem() + " " + position.getCount());
            position = position.getLink();
        }
    }
}

我认为该问题与Node1的构造函数有关,该构造函数的成员链接类型为Node1。我试图理解这些数据结构是如何工作的,而不仅仅是为我的项目使用内置的ArrayList(& APIs)。你们可以看看并指出我正确的方向。非常感谢任何帮助。

这是我的主要方法。

public class LinkedListDemo 
{
    public static void main(String[] args) 
    {
        try
        {
            LinkedList1 list = new LinkedList1();

            list.add("apples", 1);
            list.add("bananas", 2);
            list.add("cantaloupe", 3);
            System.out.println("List has "+ list.size() + " nodes.");
            list.outputList();
        }
        catch(Exception e)
        {
            System.out.println(e.getMessage());
        }
    }
}

1 个答案:

答案 0 :(得分:3)

您的size方法包含一个无限循环,解释了为什么永远不会达到输出。

while (position != null)
{
    count++;
    head = position.getLink();
}

您正在循环,直到position为null,但从不将任何内容分配给位置,而是分配给head。相反,你想做

while (position != null)
{
    count++;
    position = position.getLink();
}

现在你得到输出

List has 3 nodes.
cantaloupe 3
bananas 2
apples 1