Java 如何查看 int 数组中的元素是否为空?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/286161/
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-11 12:34:29  来源:igfitidea点击:

How can I see if an element in an int array is empty?

javaarrays

提问by kylex

example:

例子:

I want to see if array[5]holds a value or is empty.

我想看看是否array[5]持有值或为空。

采纳答案by Bill the Lizard

Elements in primitive arrays can't be empty. They'll always get initialized to something (usually 0for intarrays, but depends on how you declare the array).

原始数组中的元素不能为空。它们总是会被初始化为某些东西(通常0用于int数组,但取决于您如何声明数组)。

If you declare the array like so (for example):

如果您像这样声明数组(例如):

int [] myArray ;
myArray = new int[7] ;

then all of the elements will default to 0.

那么所有元素都将默认为0.

An alternative syntax for declaring arrays is

声明数组的另一种语法是

int[] myArray = { 12, 7, 32, 15, 113, 0, 7 };

where the initial values for an array (of size seven in this case) are given in the curly braces {}.

其中数组的初始值(在这种情况下大小为 7)在花括号中给出{}

回答by Rob Kennedy

There is no such thing as an "empty" element in a Java array. If the array's length is at least six, then element 5 exists and it has a value. If you have not assigned anything else to that location, then it will have the value zero, just like an object's uninitialized field would have.

Java 数组中没有“空”元素这样的东西。如果数组的长度至少为 6,则元素 5 存在并且它有一个值。如果您没有为该位置分配任何其他内容,则它的值将为零,就像对象的未初始化字段具有的值一样。

If it is an array of Objectdescendants, then you can check whether the element is equal to null.

如果是Object后代数组,那么可以检查元素是否等于null

回答by Vincent Ramdhanie

You have to define what you mean by empty. Depending on the datatype of the array you can decide on the semantics of empty. For example, if you have an array of ints you can decide that 0 is empty. Or if the array is of reference types then you can decide that null is empty. Then you simply check by comparing array[5] == null or array[5] == 0 etc.

你必须定义你所说的空是什么意思。根据数组的数据类型,您可以决定空的语义。例如,如果您有一个整数数组,您可以决定 0 为空。或者,如果数组是引用类型,那么您可以决定 null 为空。然后您只需通过比较 array[5] == null 或 array[5] == 0 等来检查。

回答by Terry Lacy

Primitive arrays (int, float, char, etc) are never "empty" (by which I assume you mean "null"), because primitive array elements can never be null.

原始数组(int、float、char 等)永远不会“空”(我假设您的意思是“null”),因为原始数组元素永远不会为 null。

By default, an int array usually contains 0 when allocated. However, I never rely on this (spent too much time writing C code, I guess).

默认情况下,int 数组在分配时通常包含 0。但是,我从不依赖于此(我猜花了太多时间编写 C 代码)。

One way is to pick a value that you want to treat as "uninitialized". It could be 0, or -1, or some other value that you're not going to use as a valid value. Initialize your array to that value after allocating it.

一种方法是选择一个您想视为“未初始化”的值。它可能是 0、-1 或其他一些您不会用作有效值的值。分配数组后将其初始化为该值。

Object arrays (String[] and any array of objects that extend Object), canhave null elements, so you could create an Integer[] array and initialize it to nulls. I think I like that idea better than using a magic value as described above.

对象数组(String[] 和任何扩展 Object 的对象数组)可以包含 null 元素,因此您可以创建一个 Integer[] 数组并将其初始化为 null。我想我更喜欢这个想法,而不是像上面描述的那样使用魔法值。

回答by Leigh

Create a constant to define the empty value, eg:

创建一个常量来定义空值,例如:

private static final int EMPTY = -1;

then create the array like this:

然后像这样创建数组:

int[] myArray = new int[size];
Arrays.fill(myArray, EMPTY);

then to check if an element is 'empty', do this:

然后要检查元素是否为“空”,请执行以下操作:

if (myArray[i] == EMPTY)
{
   //element i is empty
}