[英]head cannot be resolved or is not a field
I'm getting the following error:我收到以下错误:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method insert(LinkedList, String) in the type Linkedlist is not applicable for the arguments (LinkedList, int)
at uts3.Linkedlist.main(Linkedlist.java:61)
Here's the code:这是代码:
import java.io.*;
import java.util.LinkedList;
import java.util.Scanner;
public class Linkedlist {
Node head;
static int sum = 0;
static class Node{
String nama;
Node next;
Node(String n){
nama = n;
next = null;
}
}
public static LinkedList insert(LinkedList list, String nama) {
Node new_node = new Node(nama);
new_node.next = null;
if (list.head == null) {
list.head = new_node;
}
else {
Node last = list.head;
while (last.next != null) {
last = last.next;
}
last.next = new_node;
}
return list;
}
public static void printList(LinkedList list) {
Node currNode = list.head;
System.out.print("\nLinkedList: ");
while (currNode != null) {
System.out.print(currNode.nama + "");
currNode = currNode.next;
}
System.out.println("\n");
}
public static void main(String[] args) {
LinkedList list = new LinkedList();
Node head = null;
Scanner x = new Scanner( System.in );
System.out.println("Masukan 10 Nama");
for(int i=1; i<=10; i++) {
int a = x.nextInt();
list = insert(list, a);
}
printList(list);
}
}
it says that you have created a method that get String as a parameter, and you are passing int:它说您创建了一个将 String 作为参数的方法,并且您正在传递 int:
Your method definition:您的方法定义:
public static LinkedList insert(LinkedList list, String nama)
Expects String, and here:期望字符串,在这里:
System.out.println("Masukan 10 Nama");
for(int i=1; i<=10; i++) {
int a = x.nextInt();
list = insert(list, a);
}
You are passing int.你正在传递 int。 Solution: Change method's signature, or change the data passed in the loop.
解决方案:更改方法的签名,或更改循环中传递的数据。
Regarding The title: first check typo in you code.关于标题:首先检查代码中的错字。 You are passing LinkedList object, not Linkedlist to the insert method.
您将 LinkedList object 传递给 insert 方法,而不是 Linkedlist。
I will not post the correct code, but the problem in your code is,我不会发布正确的代码,但是您的代码中的问题是,
Your class name is Linkedlist
but you are creating an instance of LinkedList
which is a property of java.util.LinkedList
您的 class 名称是
Linkedlist
但您正在创建LinkedList
的实例,它是java.util.LinkedList
的属性
Solution:解决方案:
import java.util.LinkedList
and re-modify code again and compile at the end.import java.util.LinkedList
,重新修改代码,最后编译。 +1: +1:
You have another problem with method signature of insert
method. insert
方法的方法签名还有另一个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.