[英]Java unexpected type Error Required Variable Found Value
public boolean replaceEventAt(String eventStr, int position){
boolean answer = false;
if((position > 0) && (position <= events.size())){
events.get(position - 1) = eventStr;
answer = true;
}
return answer;
}
Error on fifth line where Java complains that position is a value not a variable please help 在第五行出现错误,Java抱怨位置是一个值而不是变量,请帮忙
An assignment operates between what is known as an lvalue
and an rvalue
. 分配在称为lvalue
和rvalue
。
lvalue
stands for left-hand-side-value. lvalue
代表左侧值。
rvalue
stands for right-hand-side-value. rvalue
代表右侧值。
The rvalue can be many things, like a variable, a constant, a function call expression, etc, but the left side must be assignable, so it cannot be a constant or a function call. 右值可以是很多东西,例如变量,常量,函数调用表达式等,但是左侧必须是可赋值的,因此它不能是常量或函数调用。
In your case, your rvalue is a function invocation; 在您的情况下,您的右值是一个函数调用; that won't work. 那行不通。 The compiler error message is saying exactly that. 编译器错误消息恰恰说明了这一点。
If we knew what you are trying to do we could perhaps explain more, but it is unclear from the code that you posted that it is that you want to accomplish. 如果我们知道您要尝试做的事情,我们也许可以进一步解释,但是从您发布的代码中并不清楚它是您想要完成的。 Perhaps events
is some collection, and you want to set the element at position position - 1
to eventStr
, in this case you would probably want events.set( position - 1, eventStr );
也许events
是某个集合,并且您想要将位置position - 1
的元素设置为eventStr
,在这种情况下,您可能需要events.set( position - 1, eventStr );
Left hand side must be a variable (ie we should be able to assign a value to it ). 左侧必须是一个变量(即,我们应该能够为其分配一个值)。 But in your case it is performing a get operation So it is throwing an Error 但是在您的情况下,它正在执行get操作,因此会引发错误
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.