简体   繁体   English

如何检查 Java 中是否存在变量?

[英]How can I check if a variable exists in Java?

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

inv is an int[] , and int cannot be null , since it is a primitive and not a reference. inv 是一个int[] ,而int不能是null ,因为它是一个原语而不是参考。
int s are initialized to zero in java. int在 java 中被初始化为零。

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 exactly the 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?现在,如果您使用Integer[] ,您可以使用 null 值来指示“未填充”...这是您想要的吗?

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 . Arrays 总是用元素类型的默认值开始填充 - 在Integer null

I take it that inv is an int[] .我认为inv是一个int[] You can't compare an int to null , null only applies to reference types, not primitives.您不能将intnull进行比较, null仅适用于引用类型,而不适用于原语。 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).您必须为其分配某种标志值( 0很流行,并且在创建数组时默认情况下它将具有的值),或者将inv改为Integer[]Integer是参考类型,它是null -有能力的)。

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的数组,并且 java 中的int不是 object,因此它不能具有null与每个索引的默认0进行比较。大批)..

A primitive can not be null in Java. Java 中的原语不能是 null。

Well... int is a primitive type.嗯... int是一个原始类型。 That can't be null.那不可能是 null。

You can check the size of the array:您可以检查数组的大小:

int[] arr = new int[10]; int[] arr = 新的 int[10]; System.out.println( arr.size() ); System.out.println(arr.size());

The plain arrays are indexed from 0 to their size - 1, and no value can be missing.普通的 arrays 的索引从 0 到它们的大小 - 1,并且不能缺少任何值。 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 .因此,在您的代码中,您要询问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 ) ...

You could try something like this你可以试试这样的

Integer[] inv = new Integer[10];
inv[0] = 1;
inv[1] = 2;
    ....

if(inv[3] == null)
{
    inv[3] = getSomeValue();
}

The error is because inv is an array of int , not the object wrapper Integer .该错误是因为inv是一个数组int ,而不是 object 包装器Integer Your array comes initialized for you anyway.无论如何,您的数组都会为您初始化。 If you wrote如果你写

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 (eg 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.检查无效值,如果存在,请替换它。

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM