Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 删除链接列表中的第k个元素_Java_Linked List - Fatal编程技术网

Java 删除链接列表中的第k个元素

Java 删除链接列表中的第k个元素,java,linked-list,Java,Linked List,我正在尝试编写一个代码来删除链接列表中的第k个元素。我无法理解这里的错误是什么。如果我能有人解释一下这个错误,我将不胜感激 /* Write a method delete() that takes an int argument k and deletes the kth element in a linked list if it exists. */ class Deletenode { private int N;

我正在尝试编写一个代码来删除链接列表中的第k个元素。我无法理解这里的错误是什么。如果我能有人解释一下这个错误,我将不胜感激

            /*
    Write a method delete() that takes an int argument k and deletes the kth element in a linked list if it exists.

    */
    class Deletenode
    {
        private int N;

        private class Node
        {
            String item;
            Node next;
        }

        // Building a Linked List

        Deletenode()
        {
        Node first  = new Node();
        Node second = new Node();
        Node third  = new Node();
        Node fourth = new Node();
        Node fifth  = new Node();
        Node sixth  = new Node();

        first.item  = "to";
        second.item = "be";
        third.item  = "or";
        fourth.item = "not";
        fifth.item  = "to";
        sixth.item  = "be";

        first.next = second;
        second.next = third;
        third.next = fourth;
        fourth.next = fifth;
        fifth.next = sixth;
        sixth.next = null;
    }

        public void delete(int k)
        {
            int i = 1;
            for(Node x = first; x!= null || i== --k; x = x.next,i++)
            {
                if(i == k-1)
                x = x.next.next;
            }

        }

        public void show()
        {
            for(Node x = first; x!= null; x = x.next)
            {
                System.out.print(x.item);
            }
        }

        public static void main(String[] args)
        {
            Deletenode d = new Deletenode();
            d.show();
            d.delete(3);
            d.show();

        }


    }
我得到的错误是

            deleteknode.java:44: error: cannot find symbol
            for(Node x = first; x!= null || i== --k; x = x.next,i++)
                         ^
      symbol:   variable first
      location: class Deletenode
    deleteknode.java:54: error: cannot find symbol
            for(Node x = first; x!= null; x = x.next)
                         ^
      symbol:   variable first
      location: class Deletenode
    2 errors

提前感谢

您不能将这样的代码直接放在类的主体中

first.item  = "to";
second.item = "be";
third.item  = "or";
fourth.item = "not";
fifth.item  = "to";
sixth.item  = "be";

first.next = second;
second.next = third;
third.next = fourth;
fourth.next = fifth;
fifth.next = sixth;
sixth.next = null;
将其移动到实例初始值设定项、构造函数或方法并调用它


此外,您还为类声明了一个类型参数,
Item
。它是无界的,所以不能只将
String
值分配给使用该泛型类型声明的元素


还有这个

 if(x == k-1)

没有意义,因为
x
k-1
解析为
int
值。你无法将两者进行比较。您可能想计算迭代的节点数。

是否定义了Item类?您很可能希望Item是字符串,而不是您使用它的方式的Item类型。另请参见下面索蒂里奥斯的回答,其中的参数是一个类型参数。。。我不确定我是否需要定义它。我更新了代码。。。当我将代码移动到构造函数时,我无法获得第一个构造函数的引用。我该如何解决这个问题?@shbolise在类中添加一个实例字段来保存
第一个
元素。谢谢,但这样做我有点困惑。。。你能给我一个小代码片段吗。谢谢@shbolise您的字段
N
是一个实例字段。这意味着它属于实例,每个实例都有自己的副本。创建另一个类型为节点的实例字段,并在构造函数中初始化它。是关于类和对象的Java教程。