我是java的新手,我想创建一个数组并更改数组中的特定值。我知道如果我像这样初始化一个数组:
int[] myArray = new int[4];
数组中的所有值都将设置为0.如果我想检查0值是否故意是否有任何内容(例如引用),那么将与指示该值的默认值不同默认情况下没有设置0?
答案 0 :(得分:1)
我建议您使用Integer
而不是int
的数组。然后,您可以通过将项目与null
进行比较来区分未分配的值,因为这些项目由null
初始化而不是零。
答案 1 :(得分:1)
立即用一些永远无法有意设置的值填充数组。例如,如果您知道合法值介于0到40之间,则可以执行以下操作:
int[] myArray = new int[4];
Arrays.fill(myArray, 42);
然后,您可以通过测试是否已设置元素来判断它是否为42。
答案 2 :(得分:0)
您可以尝试使用循环
将默认值设置为0
以外的值
for( int i=0;i<myArray.length;i++){
myArray[i]=1;
}
答案 3 :(得分:0)
您可以围绕数组创建自己的包装类。这允许您创建另一个内部布尔数组以跟踪修改的值:
Unhandled exception:
TlsException: Failure in usePrivateKeyBytes (OS Error:
KEY_VALUES_MISMATCH(x509_cmp.c:331), errno = 0)
#0 _SecurityContext.usePrivateKeyBytes (dart:io-patch/secure_socket_patch.dart:156)
#1 _SecurityContext.usePrivateKey (dart:io-patch/secure_socket_patch.dart:152)
#2 main (file:///usr/local/www/www.revisortextos.pt/bin/main.dart:2105:10)
<asynchronous suspension>
#3 _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:263)
#4 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:151)