[英]incompatible types: java.lang.Object cannot be converted to java.lang.String?
I am currently new to Java and have no prior programming experience, I am currently trying to code a solution to a problem proposed during my university interview :) 我目前是Java新手,没有任何编程经验,我目前正在尝试编写大学面试中提出的问题的解决方案:)
//Winner rabbit variable to hold the winner of the 'race'
String winner;
winner = yettoracequeue.element();
Background about problem: 问题背景:
I do not understand why yettoracequeue.element() is considered an object when the result is a string, eg Rabbit, and hence I am unable to assign it to the String variable that is winner. 我不明白为什么当结果是字符串(例如Rabbit)时,尚未将yettoracequeue.element()视为对象,因此我无法将其分配给优胜者的String变量。
TIA :) TIA :)
Edit: 编辑:
This is the full code 这是完整的代码
package queuepart;
import java.util.*;
public class QueuePart {
static String nextline = System.getProperty("line.separator");
public static void main(String[] args) {
//Step 1: Create LinkedList() to assign to yettoracequeue
Queue yettoracequeue = new LinkedList();
//Step 2: add rabbits to queue
int rabbitno = 1;
yettoracequeue.add("Rabbit" + rabbitno);
rabbitno++;
yettoracequeue.add("Rabbit" + rabbitno);
rabbitno++;
yettoracequeue.add("Rabbit" + rabbitno);
rabbitno++;
yettoracequeue.add("Rabbit" + rabbitno);
rabbitno++;
yettoracequeue.add("Rabbit" + rabbitno);
rabbitno++;
yettoracequeue.add("Rabbit" + rabbitno);
System.out.println(nextline + "Items in the queue" + yettoracequeue + nextline);
//Find first item in queue
System.out.println(nextline + "First item in queue is " + yettoracequeue.element());
//Assign First item in queue to racer
String winner = yettoracequeue.element();
}
}
You need a typecast: 您需要一个类型转换:
winner = (String) yettoracequeue.element();
Explanation: the way you have declared the yettoracequeue
variable, what you have is a Queue
of objects; 说明:您声明yettoracequeue
变量的方式是,对象Queue
; that is, a queue that could contain any kind of object. 也就是说, 可以包含任何对象的队列。 You have added String
objects to the queue, but you could have put any type of object into it. 您已经将String
对象添加到队列中,但是可以将任何类型的对象放入队列中。
So when you call yettoracequeue.element()
, the compiler only knows that the object is going to be an instance of java.lang.Object
or some subclass. 因此,当您调用yettoracequeue.element()
,编译器仅知道该对象将是java.lang.Object
的实例或某个子类。 (That is because every object is an instance of java.lang.Object
or some subclass!) (这是因为每个对象都是java.lang.Object
或某个子类的实例!)
But when you assign the value to winner
, the system needs to know that the object you assign is really a String
. 但是,当您将值分配给winner
,系统需要知道您分配的对象实际上是String
。 (If it was something else, then String
specific operations on it would not work.) (如果还有其他问题,则无法对它进行String
特定的操作。)
So what does the type-cast do? 那么类型转换是做什么的呢?
Well (String) yettoracequeue.element()
does a runtime type check. 好(String) yettoracequeue.element()
进行运行时类型检查。 It checks that the object returned by the method call is really a String: 它检查方法调用返回的对象是否确实是字符串:
If the runtime type check succeeds, then it treats the type of the expression as String
... and the assignment is valid. 如果运行时类型检查成功,则将表达式的类型视为String
...,并且分配有效。
If the runtime type check fails, then the runtime system throws a ClassCastException
... and your program will typically fail. 如果运行时类型检查失败,则运行时系统将引发ClassCastException
...,并且您的程序通常会失败。
Actually, there is a better way to solve the problem in this particular case. 实际上,在这种特殊情况下,有更好的方法来解决该问题。 The Queue
type is actually a generic type; Queue
类型实际上是通用类型; ie you can give it a type parameter. 即你可以给它一个类型参数。 It looks like this: 看起来像这样:
Queue<String> yettoracequeue = new LinkedList<>();
I have now declared that yettoracequeue
is a queue that contains String
objects. 我现在已经声明yettoracequeue
是一个包含String
对象的队列。 If I do that then: 如果我这样做,那么:
When I try to add (say) an Integer
object to the queue, I will get a compilation error. 当我尝试向队列中添加(例如)一个Integer
对象时,会出现编译错误。
When I call yettoracequeue.element()
the compiler will know that the queue only contains String
objects, and won't insist on the type cast when I assign the result to a String
variable. 当我调用yettoracequeue.element()
,编译器将知道队列仅包含String
对象,并且在将结果分配给String
变量时不会坚持使用yettoracequeue.element()
类型转换。
The problem in your code is that the queue is a raw type as opposed to a queue of String . 您的代码中的问题是,队列是原始类型,而不是String队列。 To avoid the type error, you have to provide the generic type argument: 为了避免类型错误,您必须提供通用类型参数:
Queue<String> yettoracequeue = new LinkedList<String>();
// ^^^^^^
You can read more about Generic Types in the Tutorial by Oracle . 您可以在Oracle教程中阅读有关泛型的更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.