Java中如何检查空元素是否为整数数组?
我对 Java 很陌生,在检查整数数组中的空元素时遇到问题。 我正在使用 Eclipse 作为编辑器,检查 null 元素的行显示错误:
抱怨的行:
if(a[i] != null) {
来自 Eclipse 的错误消息:
The operator != is undefined for the argument type(s) int, null
在 PHP 中,这没有任何问题,但在 Java 中,似乎我必须将数组类型从整数更改为到 Object 以使该行不抱怨(如下所示)
Object[] a = new Object[3];
所以我的问题是我是否仍然想声明为整数数组并且仍然想检查 null, 它的语法是什么?
代码:
public void test() {
int[] a = new int[3];
for(int i=0; i<a.length; i++) {
if(a[i] != null) { //this line complains...
System.out.println('null!');
}
}
}
I'm quite new to Java and having an issue checking null element in integer array.
I'm using Eclipse for editor and the line that checks null element is showing error:
Line that complains:
if(a[i] != null) {
Error msg from Eclipse:
The operator != is undefined for the argument type(s) int, null
In PHP, this works without any problem but in Java it seems like I have to change the array type from integer to Object to make the line not complain (like below)
Object[] a = new Object[3];
So my question is if I still want to declare as integer array and still want to check null,
what is the syntax for it?
Code:
public void test() {
int[] a = new int[3];
for(int i=0; i<a.length; i++) {
if(a[i] != null) { //this line complains...
System.out.println('null!');
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在 Java 中,
int
是原始类型,不能为null
。然而,对象存储为引用,因此,如果您声明对象引用但不创建new
对象,则该引用将为null
。Integers
是int
的对象包装器,这意味着它们可以是null
。In Java, an
int
is a primitive type and cannot benull
. Objects, however, are stored as references, so if you declare an object reference but do not make anew
object, the reference will benull
.Integers
are object wrappers aroundints
, meaning they can benull
.关于原始类型与引用类型
int
是原始类型,它与引用类型不同。只有引用类型可以具有值null
。参考文献
相关问题
关于
Integer
与int
java.lang.Integer
是实际上是一个引用类型,指定“box”类型为原始类型int
。因此,Integer
变量可以具有值null
。随着 Java 中自动装箱的引入,从
int
到Integer
的转换可以隐式完成,反之亦然。但请记住,它们是非常不同的类型,事实上,尝试拆箱null
将抛出NullPointerException
。参考文献
相关问题
关于
Integer
作为引用类型的后果一个后果已经是提到:
Integer
变量可以有一个null
值。另一个是两个Integer
上的==
运算符是引用恒等比较,而不是数值相等。只要有可能,您应该更喜欢原始类型而不是装箱类型。以下是《Effective Java 第二版》第 49 条的引述:优先使用原始类型而不是盒装原始数据(作者强调):
相关问题
==
对于Integer
和int
操作数的行为当必须使用
Integer
时==
对于Integer
和int
操作数的行为方式有一个明显的例外,即必须使用Integer
而不是int
:泛型。 Java 泛型中的类型参数必须是引用类型。所以你不能有一个List;
在 Java 中;您必须使用List
来代替。相关问题
另请参阅
关于使用适当的数据结构
如果您 < em>必须有一个允许
null
值的int[]
,那么快速的答案是使用Integer[]
。由于您现在拥有一个引用类型数组,因此某些元素可能为null
。请注意使用引用类型的所有后果,否则您可能会遇到意外。不过,此时我会认真考虑使用
List
(请参阅Effective Java 2nd Edition:优先使用列表而不是数组)。列表比数组功能丰富得多,并且它与更大的 Java 集合框架可以很好地互操作。API 参考
java.util.List
On primitive vs reference types
An
int
is a primitive type, which is distinct from a reference type. Only reference types can have the valuenull
.References
Related questions
On
Integer
vsint
java.lang.Integer
is in fact a reference type, the designated "box" type for the primitive typeint
. Thus, anInteger
variable can have the valuenull
.With the introduction of autoboxing in Java, conversions from
int
toInteger
and vice versa can be done implicitly. But do keep in mind that they are very different types, and in fact an attempt to unboxnull
will throwNullPointerException
.References
Related questions
On consequences of
Integer
being a reference typeOne consequence is already mentioned: an
Integer
variable can have anull
value. Another one is that the==
operator on twoInteger
is a reference identity comparison, not numerical equality.Whenever possible, you should prefer primitive types to boxed types. Here's a quote from Effective Java 2nd Edition, Item 49: Prefer primitive types to boxed primitives (emphasis by author):
Related questions
==
behaves forInteger
andint
operandsWhen
Integer
must be usedThere is one glaring exception where
Integer
must be used overint
: generics. Type parameters in Java generics must be reference types. So you can NOT have aList<int>
in Java; you must use aList<Integer>
instead.Related questions
See also
On using the appropriate data structure
If you must have an
int[]
that permitsnull
values, then the quick answer is to useInteger[]
. Since you now have an array of reference types, some elements can benull
. Be aware of all the consequences of working with reference types, or you may come across surprises.At this point, however, I'd seriously consider using a
List<Integer>
instead (see Effective Java 2nd Edition: Prefer lists to arrays). Lists are much more feature-rich than arrays, and it interoperates well with the larger Java Collections Framework.API references
java.util.List
“int”不能为空。整数(它是一个对象)可以。因此,正如 @Justin 所说,Integer[] 将允许您测试 null,但如果 int[] 适合您,那么您不需要费心测试 null,因为它不会发生。
An 'int' cannot be null. An Integer (which is an object) can. So, as @Justin said, Integer[] will allow you to test for null, but if int[] is working for you, then you don't need to bother testing for null because it can't happen.
int是java中的基本类型,不能为null。只有对象可以为 null。
int is a primitive type in java, and cannot be null. Only objects can be null.
您可以像这样使用 arr[i] != '\0' 。这对我有用。
You can use arr[i] != '\0' like this. It worked for me.