如果空元素是Java中的整数数组,如何检查空元素?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3033633/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 15:48:32  来源:igfitidea点击:

How to check null element if it is integer array in Java?

javaarraysnullint

提问by Meow

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:

我对 Java 很陌生,在检查整数数组中的空元素时遇到问题。我使用 Eclipse 作为编辑器,检查空元素的行显示错误:

Line that complains:

抱怨的行:

if(a[i] != null) {

Error msg from Eclipse:

来自 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)

在 PHP 中,这没有任何问题,但在 Java 中,似乎我必须将数组类型从整数更改为对象,以使行不抱怨(如下所示)

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!');
            }
        }
    }

采纳答案by Justin Ardini

In Java, an intis 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 newobject, the reference will be null.

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

Integersare object wrappers around ints, meaning they can be null.

Integers是周围的对象包装器ints,这意味着它们可以是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!');
            }
        }
    }

回答by Greg

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

int 是 java 中的原始类型,不能为 null。只有对象可以为空。

回答by dty

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”不能为空。一个整数(它是一个对象)可以。因此,正如@Justin 所说,Integer[] 将允许您测试 null,但是如果 int[] 对您有用,那么您就无需费心测试 null,因为它不可能发生。

回答by polygenelubricants

On primitive vs reference types

关于原始类型与引用类型

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

Anint是一种原始类型,它不同于引用类型。只有引用类型可以有值null

References

参考

Related questions

相关问题



On Integervs int

IntegerVSint

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

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

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

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

References

参考

Related questions

相关问题



On consequences of Integerbeing a reference type

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

One consequence is already mentioned: an Integervariable can have a nullvalue. Another one is that the ==operator on two Integeris a reference identity comparison, not numerical equality.

已经提到了一个结果:Integer变量可以有一个null值。另一个是==对二的操作符Integer是引用身份比较,而不是数字相等。

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

只要有可能,您应该更喜欢原始类型而不是装箱类型。这里引用自Effective Java 2nd Edition,Item 49:Prefer prime types to boxed primes(作者强调):

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

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

Related questions

相关问题



When Integermust be used

什么时候Integer必须使用

There is one glaring exception where Integermust 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.

有一个明显的例外Integer必须用于int:泛型。Java 泛型中的类型参数必须是引用类型。所以你不能List<int>在 Java 中有一个;你必须使用 aList<Integer>代替。

Related questions

相关问题

See also

也可以看看



On using the appropriate data structure

关于使用适当的数据结构

If you musthave an int[]that permits nullvalues, 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.

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

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.

然而,在这一点上,我会认真考虑使用 aList<Integer>代替(参见Effective Java 2nd Edition: Prefer lists to arrays)。列表的功能比数组丰富得多,并且它可以与更大的 Java 集合框架很好地互操作。

API references

API 参考

回答by DecipherX

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

您可以像这样使用 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 != '##代码##'){
            System.out.println(n);
        }else
            System.out.println("Null");
    }
}
}