java 如何检查Java中是否存在变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6792132/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I check if a variable exists in Java?
提问by Jon Skeet
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
回答by amit
回答by Jon Skeet
I'm assuming inv
is an int[]
.
我假设inv
是一个int[]
.
There's no such concept as a value "existing" or not in an array. For example:
数组中没有值“存在”或不存在这样的概念。例如:
int[] x = new int[5];
has exactlythe same contents as:
有确切的相同的内容:
int[] x = new int[5];
x[3] = 0;
Now if you used an Integer[]
you could use a null value to indicate "unpopulated"... is that what you want?
现在,如果您使用 anInteger[]
您可以使用空值来指示“未填充”……这是您想要的吗?
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 as Integer
.
数组总是用元素类型的默认值开始填充 - 这就是null
引用类型的情况,例如Integer
.
回答by Sorceror
I'm assuming from error message, that inv[]
is array of int
, and int
in java is not an object, so it cannot have null
value.. You have to compare it with 0
(default value on each index of empty int array)..
我从错误消息中假设,那inv[]
是数组int
,int
在java中不是对象,所以它不能有null
值..你必须将它与0
(空int数组的每个索引上的默认值)进行比较。
回答by T.J. Crowder
I take it that inv
is an int[]
. You can't compare an int
to null
, 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 make inv
an Integer[]
instead (Integer
being a reference type, it is null
-able).
我认为这inv
是一个int[]
. 您无法比较int
to null
,null
仅适用于引用类型,而不适用于原语。您必须改为为它分配某种标志值(0
流行,以及创建数组时默认情况下的值),或者创建inv
一个Integer[]
替代(Integer
作为引用类型,它是null
-able)。
回答by fyr
A primitive can not be null in Java.
Java 中的原语不能为空。
回答by Bala R
You could try something like this
你可以试试这样的
Integer[] inv = new Integer[10];
inv[0] = 1;
inv[1] = 2;
....
if(inv[3] == null)
{
inv[3] = getSomeValue();
}
回答by Paul
The error is because inv
is an array of int
, not the object wrapper Integer
. Your array comes initialized for you anyway. If you wrote
错误是因为inv
是 的数组int
,而不是对象包装器Integer
。无论如何,您的数组已为您初始化。如果你写
int[] inv = new int[5];
you will have an array of 5 zeroes.
您将拥有一个包含 5 个零的数组。
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.
您应该使用一些您知道无效的值自己初始化您的数组(例如,如果您有一个年龄数组,负值将是无效的)。检查无效值,如果存在,则替换它。
回答by Ondra ?i?ka
Well... int
is a primitive type. That can't be null.
嗯...int
是一个原始类型。那不能为空。
You can check the size of the array:
您可以检查数组的大小:
int[] arr = new int[10]; System.out.println( arr.size() );
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
is null
, which can't happen - either it's a number or it will cause ArrayOutOfBoundsException
.
普通数组的索引从 0 到它们的大小 - 1,并且不能缺少任何值。因此,在您的代码中,您询问 type 的第一个成员是否int
为null
,这不可能发生 - 要么是数字,要么会导致ArrayOutOfBoundsException
.
If you want to have a "sparse array" similar to what PHP or JavaScript, you need a Map
:
如果你想要一个类似于 PHP 或 JavaScript 的“稀疏数组”,你需要一个Map
:
Map<Integer, Integer> map = new HashMap();
map.put( 1, 324 );
if( map.get( 2 ) == null ) ...
回答by Hunter McMillen
primitive types can't be compared to null.
原始类型不能与 null 进行比较。
You can test if the number if > 0 to see if a value exists:
您可以测试数字 if > 0 以查看值是否存在:
if(inv[0] <= 0)
{
inv[0]=map.getTileId(tileX-1, tileY-1, 0);
}