Java中如何检查变量是否存在?
仅当变量中还没有任何内容时,我才想写入该变量。到目前为止,这是我的代码。
if (inv[0] == null) {
inv[0]=map.getTileId(tileX-1, tileY-1, 0);
}
它给了我这个错误:
java.lang.Error: Unresolved compilation problem:
The operator == is undefined for the argument type(s) int, null
I want to write to a variable only if there isn't anything already there. Here is my code so far.
if (inv[0] == null) {
inv[0]=map.getTileId(tileX-1, tileY-1, 0);
}
It gives me this error:
java.lang.Error: Unresolved compilation problem:
The operator == is undefined for the argument type(s) int, null
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
inv 是一个
int[]
,并且int
不能为null
,因为它是一个 原始,而不是参考。int
在 java 中被初始化为零。inv is an
int[]
, andint
cannot benull
, since it is a primitive and not a reference.int
s are initialized to zero in java.我假设
inv
是一个int[]
。不存在值“存在”或不在数组中这样的概念。例如:
与以下内容完全相同:
现在,如果您使用
Integer[]
,您可以使用空值来指示“未填充”...这就是您的意思想?数组始终填充起始元素类型的默认值 - 对于引用类型(例如
Integer
),该默认值是null
。I'm assuming
inv
is anint[]
.There's no such concept as a value "existing" or not in an array. For example:
has exactly the same contents as:
Now if you used an
Integer[]
you could use a null value to indicate "unpopulated"... is that what you want?Arrays are always filled with the default value for the element type to start with - which is
null
in the case of reference types such asInteger
.我认为
inv
是一个int[]
。您无法将int
与null
进行比较,null
仅适用于引用类型,不适用于基元。您必须为其分配某种标志值(0
很流行,并且在创建数组时默认情况下具有该值),或者将inv
设为相反,Integer[]
(Integer
是引用类型,可以为null
)。I take it that
inv
is anint[]
. You can't compare anint
tonull
,null
only applies to reference types, not primitives. You have to either assign it some kind of flag value instead (0
being popular, and the value it will have by default when you create the array), or makeinv
anInteger[]
instead (Integer
being a reference type, it isnull
-able).我从错误消息中假设
inv[]
是int
的数组,而 java 中的int
不是一个对象,所以它不能有null
值..您必须将其与0
(空 int 数组的每个索引上的默认值)进行比较。I'm assuming from error message, that
inv[]
is array ofint
, andint
in java is not an object, so it cannot havenull
value.. You have to compare it with0
(default value on each index of empty int array)..Java 中的原语不能为 null。
A primitive can not be null in Java.
嗯...
int
是一种原始类型。那不能为空。您可以检查数组的大小:
int[] arr = new int[10];
System.out.println( arr.size() );
普通数组的索引范围是从 0 到其大小 - 1,并且任何值都不能丢失。
因此,在您的代码中,您询问
int
类型的第一个成员是否为null
,这是不可能发生的 - 要么它是一个数字,要么会导致ArrayOutOfBoundsException
。如果你想要一个类似于 PHP 或 JavaScript 的“稀疏数组”,你需要一个
Map
:Well...
int
is a primitive type. That can't be null.You can check the size of the array:
int[] arr = new int[10];
System.out.println( arr.size() );
The plain arrays are indexed from 0 to their size - 1, and no value can be missing.
So in your code, you are asking whether the first member of type
int
isnull
, which can't happen - either it's a number or it will causeArrayOutOfBoundsException
.If you want to have a "sparse array" similar to what PHP or JavaScript, you need a
Map
:你可以尝试这样的事情
You could try something like this
该错误是因为
inv
是一个int
数组,而不是对象包装器Integer
。无论如何,你的数组都会为你初始化。如果你这样写,你将得到一个包含 5 个零的数组。
您应该使用一些您知道无效的值自行初始化数组(例如,如果您有一个年龄数组,则负值将无效)。检查是否存在无效值,如果存在,请将其替换。
The error is because
inv
is an array ofint
, not the object wrapperInteger
. Your array comes initialized for you anyway. If you wroteyou will have an array of 5 zeroes.
You should initialize your array yourself using some value that you know is invalid (e.g. if you had an array of ages, a negative value would be invalid). Check for the invalid value and if it's there, replace it.
原始类型不能与 null 进行比较。
您可以测试该数字是否> if > 0 查看某个值是否存在:
primitive types can't be compared to null.
You can test if the number if > 0 to see if a value exists: