意外的所需类型变量值发现 [英] Unexpected type required variable found value
本文介绍了意外的所需类型变量值发现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public class example
{
public ArrayList<Integer> ToFill = new ArrayList<>();
public void Alter(int Value , int Position)
{
ToFill.get(Position) = Value ; // this line has an error
}
}
有关某种原因,这code给出编译错误,任何人都可以解释为什么?
For some reason this code gives compilation Error ,could anyone explain why?
推荐答案
ToFill.get(位置)
返回一个值,其中转让的左侧必须是一个变量。相反,使用 设置(索引,元素)
如下:
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;
作为一个方面说明,总是使用Java命名约定:
As a side note, always use Java naming convention:
-
toFill
而不是ToFill
-
修改
而不是阿尔特
-
位置
而不是位置
。 -
值
而不是值
。
toFill
instead ofToFill
alter
instead ofAlter
position
instead ofPosition
.value
instead ofValue
.
这篇关于意外的所需类型变量值发现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文