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

发布于 2024-11-25 10:14:32 字数 298 浏览 2 评论 0原文

仅当变量中还没有任何内容时,我才想写入该变量。到目前为止,这是我的代码。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

掌心的温暖 2024-12-02 10:14:32

inv 是一个 int[],并且 int 不能为 null,因为它是一个 原始,而不是参考。

int 在 java 中被初始化为零。

inv is an int[], and int cannot be null, since it is a primitive and not a reference.

ints are initialized to zero in java.

预谋 2024-12-02 10:14:32

我假设 inv 是一个 int[]

不存在值“存在”或不在数组中这样的概念。例如:

int[] x = new int[5];

与以下内容完全相同

int[] x = new int[5];
x[3] = 0;

现在,如果您使用Integer[],您可以使用空值来指示“未填充”...这就是您的意思想?

数组始终填充起始元素类型的默认值 - 对于引用类型(例如 Integer),该默认值是 null

I'm assuming inv is an 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?

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.

糖果控 2024-12-02 10:14:32

我认为 inv 是一个 int[]。您无法将 intnull 进行比较,null 仅适用于引用类型,不适用于基元。您必须为其分配某种标志值(0 很流行,并且在创建数组时默认情况下具有该值),或者将 inv 设为相反,Integer[]Integer 是引用类型,可以为 null)。

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).

蓝海似她心 2024-12-02 10:14:32

我从错误消息中假设 inv[]int 的数组,而 java 中的 int 不是一个对象,所以它不能有 null 值..您必须将其与 0 (空 int 数组的每个索引上的默认值)进行比较。

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)..

风透绣罗衣 2024-12-02 10:14:32

Java 中的原语不能为 null。

A primitive can not be null in Java.

世界等同你 2024-12-02 10:14:32

嗯... int 是一种原始类型。那不能为空。

您可以检查数组的大小:

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

普通数组的索引范围是从 0 到其大小 - 1,并且任何值都不能丢失。
因此,在您的代码中,您询问 int 类型的第一个成员是否为 null,这是不可能发生的 - 要么它是一个数字,要么会导致 ArrayOutOfBoundsException

如果你想要一个类似于 PHP 或 JavaScript 的“稀疏数组”,你需要一个 Map

Map<Integer, Integer> map = new HashMap();
map.put( 1, 324 );
if( map.get( 2 ) == null ) ...

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 is null, which can't happen - either it's a number or it will cause ArrayOutOfBoundsException.

If you want to have a "sparse array" similar to what PHP or JavaScript, you need a Map:

Map<Integer, Integer> map = new HashMap();
map.put( 1, 324 );
if( map.get( 2 ) == null ) ...
无边思念无边月 2024-12-02 10:14:32

你可以尝试这样的事情

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

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

You could try something like this

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

if(inv[3] == null)
{
    inv[3] = getSomeValue();
}
生生不灭 2024-12-02 10:14:32

该错误是因为 inv 是一个 int 数组,而不是对象包装器 Integer。无论如何,你的数组都会为你初始化。如果你这样写,

int[] inv = new int[5];

你将得到一个包含 5 个零的数组。

您应该使用一些您知道无效的值自行初始化数组(例如,如果您有一个年龄数组,则负值将无效)。检查是否存在无效值,如果存在,请将其替换。

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

int[] inv = new int[5];

you 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.

弥枳 2024-12-02 10:14:32

原始类型不能与 null 进行比较。

您可以测试该数字是否> if > 0 查看某个值是否存在:

if(inv[0] <= 0)
{
    inv[0]=map.getTileId(tileX-1, tileY-1, 0);
}

primitive types can't be compared to null.

You can test if the number if > 0 to see if a value exists:

if(inv[0] <= 0)
{
    inv[0]=map.getTileId(tileX-1, tileY-1, 0);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文