java array Can you check if a value exists in a specific index?

Asked 2 years ago, Updated 2 years ago, 77 views

int[ ] [ ] array = new int[3][3]; when If the value of array[1][2] exists, can we make it true if it does not exist?

java array

2022-09-22 18:47

1 Answers

If you create an array of scalar types such as int, initialize the elements to zero.

On the other hand, created an array with the reference type, factors such as integer null to reset.

You can check if it is 0 or null.

jshell> int[][] array = new int[3][3]
array ==> int[3][] { int[3] { 0, 0, 0 }, int[3] { 0, 0, 0 }, int[3] { 0, 0, 0 } }

jshell> array[1][2]
$2 ==> 0

jshell> array[1][2] != 0
$3 ==> false

jshell> Integer[][] array2 = new Integer[3][3]
array2 ==> Integer[3][] { Integer[3] { null, null, null }, I ... [3] [3] { null, null, null } }

jshell> array2[1][2]
$5 ==> null

jshell> array2[1][2] != null
$6 ==> false


2022-09-22 18:47

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.