英文:
Constructor type mismatch in java
问题
我正在自学Java中的链表,并编写了一个基本程序。我遇到了与构造函数相关的错误,无法理解其中的问题。以下是我的代码:
import java.util.*;
public class prac {
public class Linked {
public void display(Node head) {
if (head == null) {
return;
}
Node current = head;
while (current != null) {
System.out.print(current.data + " --> ");
current = current.next;
}
System.out.println(current);
}
private class Node {
int data;
Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}
}
public static void main(String[] args) {
Node head = new Node(10);
Node second = new Node(11);
Node third = new Node(5);
Node fourth = new Node(1);
head.next = second;
second.next = third;
third.next = fourth;
Linked linklist = new Linked();
linklist.display(head);
}
}
一些错误如下所示:
错误:在类Node中无法应用于给定类型的构造函数Node;
Node fourth = new Node(1);
^
要求:无参数
找到:int
原因:实际参数列表的长度与形式参数列表不同
错误:无法从静态上下文引用非静态变量this
Linked linklist = new Linked();
^
prac.java:40: 错误:不兼容的类型:无法将Node转换为prac.Linked.Node
linklist.display(head);
有人可以解释一下如何解决这个错误以及错误背后的原因吗?我陷入了困境。
英文:
I'm self-learning linked list in java and I was writing a basic program. I'm getting errors related to constructors that I cannot understand. Here is my code:
import java.util.*;
public class prac{
public class Linked{
public void display(Node head)
{
if(head==null) // list is empty
{
return ;
}
Node current=head;
while(current!=null)
{ System.out.print(current.data+ " --> ");
current=current.next;
}
System.out.println(current);
}
private class Node{
int data;
Node next;
public Node(int data)
{
this.data=data;
this.next=null;
}
}
}
public static void main(String[] args)
{
Node head=new Node(10);
Node second=new Node(11);
Node third=new Node(5);
Node fourth=new Node(1);
head.next=second;
second.next=third;
third.next=fourth;
Linked linklist=new Linked();
linklist.display(head);
}
}
Some of the errors are this:
error: constructor Node in class Node cannot be applied to given types;
Node fourth=new Node(1);
^
required: no arguments
found: int
reason: actual and formal argument lists differ in length
error: non-static variable this cannot be referenced from a static context
Linked linklist=new Linked();
^
prac.java:40: error: incompatible types: Node cannot be converted to prac.Linked.Node
linklist.display(head);
Can anyone please explain how to solve this error and the reason behind it? I'm stuck here.
/
答案1
得分: 0
如果您有一个内部类,并且您有一个变量类型相同,那么将优先考虑
Node current=head;
这指的是私有内部类
在您的主方法中
Node head=new Node(10)
这指的是另一个类
要解决这个问题,一个选项是更改内部类名称,另一个选项是创建一个构造函数并将所有值复制到该对象中
private class Node{
int data;
Node next;
public Node(mypackage.Node node) {
this.data = node.data;
this.next = node.next;
}
public Node(int data)
{
this.data=data;
this.next=null;
}
}
然后将您的显示函数更改为
Node current=new Node(head);
在此代码中,mypackage
被假定为您自己的包或第三方库。
英文:
If you have an inner class and you have a variable type as same, then that will take priority
Node current=head;
this refers to the private inner class
In your main method
Node head=new Node(10)
this refers to a different class
To solve this issue, one option, change the inner class name, other option create a constructor and copy all the values to that object
private class Node{
int data;
Node next;
public Node(mypackage.Node node) {
this.data = node.data;
this.next = node.next;
}
public Node(int data)
{
this.data=data;
this.next=null;
}
}
Then change you display function as
Node current=new Node(head);
In this code mypackage
is assumed to be your own package or a third party library
答案2
得分: 0
你无法从更高的范围访问Node
类,因为它对于Linked
类是私有的。只有在链接类内部,你才能创建一个Node对象,因为它的访问受限制。
在我的解决方案中,我最初拆分了这些类,所以Linked
不再在Prac
类内部。否则,由于Prac
具有主方法public static void main(String[] args)
,你的所有方法都必须自然而然地是静态的。
此外,我选择将Node构造函数保持为私有,但公开了一个addNode(int value)
方法,供你调用。
解决方案在这里以供参考。
英文:
You are not able to access the Node
class from higher scope because it is private
to the Linked
class. Only within the linked class can you actually create a Node object due to it's access restriction.
In my solution, I initially split the classes, so Linked
is no longer inside the Prac
class. Otherwise all of your methods would have to be naturally static
due to Prac
having the main method
public static void main(String[] args)
.
Additionally, I chose to keep the Node constructor private, but expose an addNode(int value)
method publicly which you can call.
The solution is here to help.
专注分享java语言的经验与见解,让所有开发者获益!
评论