意外类型需要变量找到值(Unexpected type required variable found value)
public class example
{
public ArrayList ToFill = new ArrayList<>();
public void Alter(int Value , int Position)
{
ToFill.get(Position) = Value ; // this line has an error
}
}
由于某种原因,此代码提供编译错误,任何人都可以解释为什么?
public class example
{
public ArrayList ToFill = new ArrayList<>();
public void Alter(int Value , int Position)
{
ToFill.get(Position) = Value ; // this line has an error
}
}
For some reason this code gives compilation Error ,could anyone explain why?
原文:https://stackoverflow.com/questions/14834224
更新时间:2019-12-02 07:07
最满意答案
ToFill.get(Position)返回一个值,其中赋值的左侧必须是变量。 相反,使用set(index, element)如下:
ToFill.set(Position, Value);
但是,您所做的只有在使用数组时才有效,例如:
Integer[] array = ...
array[Position] = Value;
作为旁注,请始终使用Java命名约定:
toFill而不是ToFill
alter而不是Alter
position而不是Position 。
value而不是Value 。
ToFill.get(Position) returns a value where the left-hand side of the assignment must be a variable. Instead, use set(index, element) as follows:
ToFill.set(Position, Value);
However, what you are doing is only valid if you are using arrays, for example:
Integer[] array = ...
array[Position] = Value;
As a side note, always use Java naming convention:
toFill instead of ToFill
alter instead of Alter
position instead of Position.
value instead of Value.
2013-02-12
相关问答
传递给foreach的参数是执行代码块( Unit )的结果,而不是函数。 删除外部括号(它们并没有真正伤害任何东西,但是不必要并且看起来很丑陋),并在开头添加_ => : (0 to t).foreach { _ =>
...
println(getBestSolution(sumList))
}
这是创建未命名函数的正确语法。 之前的东西=>是函数接受的参数列表。 在您的情况下,您可以在其中放置下划线,因为您不需要参数的值。 或者你可以给它一个名字,如果你需要对它做一些事情,例
...
对不起,但我认为你不能使它工作。 问题是Scala(如Java)使用类型擦除。 这意味着所有泛型只有一种类型(可能除了现在不重要的价值型专业化)。 这意味着宏只对所有E扩展一次,而对于用户提供的每个E专门化来说只扩展一次。 而且没有办法表达一个限制,即一些泛型类型E必须有一个给定签名的构造函数(如果有的话 - 你首先不需要你宏)。 所以显然它不能工作,因为编译器不能为泛型类型E生成构造函数调用。 所以编译器说的是,为了生成一个构造函数调用,它需要一个真实的类而不是泛型E 换句话说,宏不是一个神奇的
...
由{}分隔的代码块计算其中的最后一个表达式。 这里,你的最后一个表达式是一个定义( def Count ),它的计算结果为Unit 。 因此,将您期望评估的表达式移动到最后。 A code block delimited by {} evaluates to the last expression inside it. Here, your last expression is a definition (def Count), which evaluates to Unit. So move t
...
问题出在这里: myStr.charAt(j) += reversed;
左侧是一个值。 不是变量。 这就是为什么你不能+=它。 虽然它挫败了学习如何做到这一点的目的,但你可以这样做: myStr = new StringBuffer(myStr).reverse().toString();
The problem is here: myStr.charAt(j) += reversed;
The left-hand-side is a value. Not a variable. That
...
ToFill.get(Position)返回一个值,其中赋值的左侧必须是变量。 相反,使用set(index, element)如下: ToFill.set(Position, Value);
但是,您所做的只有在使用数组时才有效,例如: Integer[] array = ...
array[Position] = Value;
作为旁注,请始终使用Java命名约定: toFill而不是ToFill alter而不是Alter position而不是Position 。 value而不是Va
...
这个 aux1[],aux2[]
在 System.out.println (Par_impar(vv1,vv2,n,aux1[],aux2[]));
是无效的java表示法。 使用 System.out.println (Par_impar(vv1,vv2,n,aux1,aux2));
将变量作为参数传递给方法。 This aux1[],aux2[]
in System.out.println (Par_impar(vv1,vv2,n,aux1[],aux2[]));
is not v
...
在助手类中,在类初始化中定义类型B 但是在方法>或
def >[B](that: List[B]): Boolean = input.size > that.size
def
}
编辑 既然你问过为什么其他方式不可能,请考虑以下示例。 Li
...
+++y被解释为++运算符,后跟+y 。 +y与-y一样有效,但++运算符需要一个变量来操作(它不能增加一个值),而+y被认为是一个值(执行了加法操作)。 +-+y为0 + (0 - (0 + y)) ,并且它没有增加或减少运算符,因此即使该运算将整个表达式转换为值(而不是变量引用)没有效果。 +++y is interpreted as the ++ operator followed by +y. +y is as valid as -y is, but the ++ operator exp
...
问题在于(s:String) => Unit正在返回Unit.type - 而是将其更改为(s:String) => () (或者如果您不打算返回Function1[String, Unit]从你的方法。) 换一种方式: def doSomething = (s:String) => Unit
是真的: def doSomething: (String) => Unit.type =
// A function that takes a string and returns the comp
...
问题在于这两行: jp.getX()=jp.getWidth()-10;
jp.getY()=yPos;
我假设getX和getY返回一些x和y变量。 但是,您不能以这种方式改变它们,您需要创建一个setter方法或直接访问它们并修改它们。 就像是 : public void setX(int x)
{
this.x = x;
}
然后你会这样做 jp.setX(someValue);
或者如果该字段不是私有的,您可以直接执行: jp.x = someValue;
错误消息“req
...