How to check if a variable exists in Java?

I want to write a variable only if it is already gone. Here is my code so far.

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

This gives me this error:

java.lang.Error: Unresolved compilation problem: 
The operator == is undefined for the argument type(s) int, null
+5
source share
9 answers

I guess that invis int[].

There is no such thing as an existing value or not in an array. For instance:

int[] x = new int[5];

has exactly the same content as:

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

Now, if you used Integer[], you could use a null value to indicate "unvisited" ... is that what you want?

, - null , Integer.

+5

inv int[], int null, , .
int java.

+9

, inv int[]. int null, null , . - (0 , , ) inv a Integer[] (Integer , null -able).

+2

, inv[] int, int java , null. 0 ( int).

+2

Java .

0

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

-

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

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

, inv - int, Integer. , .

int[] inv = new int[5];

5 .

, , , , (, , ). .

0

.

You can check if there is an if> 0 number to see if the value exists:

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

All Articles