Java中如何检查空元素是否为整数数组?

发布于 2024-09-05 16:38:44 字数 691 浏览 4 评论 0原文

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

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

发布评论

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

评论(5

黯淡〆 2024-09-12 16:38:46

在 Java 中,int 是原始类型,不能为 null。然而,对象存储为引用,因此,如果您声明对象引用但不创建new对象,则该引用将为null

Integersint 的对象包装器,这意味着它们可以是 null

public void test() {
        Integer[] a = new Integer[3];
        for(int i=0; i<a.length; i++) {
            if(a[i] != null) { //should now compile
                System.out.println('null!');
            }
        }
    }

In Java, an int is a primitive type and cannot be null. Objects, however, are stored as references, so if you declare an object reference but do not make a new object, the reference will be null.

Integers are object wrappers around ints, meaning they can be null.

public void test() {
        Integer[] a = new Integer[3];
        for(int i=0; i<a.length; i++) {
            if(a[i] != null) { //should now compile
                System.out.println('null!');
            }
        }
    }
紫﹏色ふ单纯 2024-09-12 16:38:46

关于原始类型与引用类型

int 是原始类型,它与引用类型不同。只有引用类型可以具有值 null

参考文献

  • JLS 4.2 基元类型和值
  • < a href="http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.3" rel="nofollow noreferrer">JLS 4.3 参考类型和值

相关问题


关于 Integerint

java.lang.Integer 是实际上是一个引用类型,指定“box”类型为原始类型int。因此,Integer 变量可以具有值null

随着 Java 中自动装箱的引入,从 intInteger 的转换可以隐式完成,反之亦然。但请记住,它们是非常不同的类型,事实上,尝试拆箱 null 将抛出 NullPointerException

参考文献

相关问题


关于 Integer 作为引用类型的后果

一个后果已经是提到:Integer 变量可以有一个 null 值。另一个是两个Integer 上的== 运算符是引用恒等比较,而不是数值相等。

System.out.println(new Integer(0) == new Integer(0)); // prints "false"

只要有可能,您应该更喜欢原始类型而不是装箱类型。以下是《Effective Java 第二版》第 49 条的引述:优先使用原始类型而不是盒装原始数据(作者强调):

总之,只要有选择,就优先使用基元而不是盒装基元。原始类型更简单、更快。如果您必须使用盒装基元,请小心! 自动装箱减少了使用装箱基元的冗长性,但没有减少危险。当您的程序使用 == 运算符比较两个装箱基元时,它会进行身份比较,这几乎肯定不是您想要的。当您的程序执行涉及装箱和拆箱基元的混合类型计算时,它会进行拆箱,并且当您的程序进行拆箱时,它可能会抛出NullPointerException。最后,当您的程序装箱原始值时,可能会导致昂贵且不必要的对象创建。

相关问题


当必须使用 Integer

== 对于 Integerint 操作数的行为方式有一个明显的例外,即必须使用 Integer 而不是 int:泛型。 Java 泛型中的类型参数必须是引用类型。所以你不能有一个 List;在 Java 中;您必须使用 List 来代替。

相关问题

另请参阅


关于使用适当的数据结构

如果您 < em>必须有一个允许null值的int[],那么快速的答案是使用Integer[]。由于您现在拥有一个引用类型数组,因此某些元素可能为 null。请注意使用引用类型的所有后果,否则您可能会遇到意外。

不过,此时我会认真考虑使用 List (请参阅Effective Java 2nd Edition:优先使用列表而不是数组)。列表比数组功能丰富得多,并且它与更大的 Java 集合框架可以很好地互操作。

API 参考

On primitive vs reference types

An int is a primitive type, which is distinct from a reference type. Only reference types can have the value null.

References

Related questions


On Integer vs int

java.lang.Integer is in fact a reference type, the designated "box" type for the primitive type int. Thus, an Integer variable can have the value null.

With the introduction of autoboxing in Java, conversions from int to Integer and vice versa can be done implicitly. But do keep in mind that they are very different types, and in fact an attempt to unbox null will throw NullPointerException.

References

Related questions


On consequences of Integer being a reference type

One consequence is already mentioned: an Integer variable can have a null value. Another one is that the == operator on two Integer is a reference identity comparison, not numerical equality.

System.out.println(new Integer(0) == new Integer(0)); // prints "false"

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

In summary, use primitives in preference to boxed primitive whenever you have the choice. Primitive types are simpler and faster. If you must use boxed primitives, be careful! Autoboxing reduces the verbosity, but not the danger, of using boxed primitives. When your program compares two boxed primitives with the == operator, it does an identity comparison, which is almost certainly not what you want. When your program does mixed-type computations involving boxed and unboxed primitives, it does unboxing, and when your program does unboxing, it can throw NullPointerException. Finally, when your program boxes primitive values, it can result in costly and unnecessary object creations.

Related questions


When Integer must be used

There is one glaring exception where Integer must be used over int: generics. Type parameters in Java generics must be reference types. So you can NOT have a List<int> in Java; you must use a List<Integer> instead.

Related questions

See also


On using the appropriate data structure

If you must have an int[] that permits null values, then the quick answer is to use Integer[]. Since you now have an array of reference types, some elements can be null. 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

日记撕了你也走了 2024-09-12 16:38:46

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

打小就很酷 2024-09-12 16:38:46

int是java中的基本类型,不能为null。只有对象可以为 null。

int is a primitive type in java, and cannot be null. Only objects can be null.

好倦 2024-09-12 16:38:46

您可以像这样使用 arr[i] != '\0' 。这对我有用。

public class IntNullCheck {
public static void main(String[] args) {

    int[] intarr = new int[5];
    intarr[0] = 7;
    intarr[3] = 9;

    for(int n : intarr){
        if(n != '\0'){
            System.out.println(n);
        }else
            System.out.println("Null");
    }
}
}

You can use arr[i] != '\0' like this. It worked for me.

public class IntNullCheck {
public static void main(String[] args) {

    int[] intarr = new int[5];
    intarr[0] = 7;
    intarr[3] = 9;

    for(int n : intarr){
        if(n != '\0'){
            System.out.println(n);
        }else
            System.out.println("Null");
    }
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文