java 如何检查数组中的元素是否存在于Java中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/622628/
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
How to check if an element in array exists in Java
提问by Click Upvote
If I have an intarray structured like this:
如果我有一个这样int结构的数组:
private int[][] map = new int[400][400];
And I try to retrieve
我试着找回
map[100][200]
And that element isn't initialized, will i get a compiler/runtime error or will it return null? And is there any function to check if a given element/index exists/has been set?
并且该元素未初始化,我会收到编译器/运行时错误还是返回 null?是否有任何功能可以检查给定的元素/索引是否存在/已设置?
回答by Alnitak
As your array declaration is of a primitive type you won't get any compiler or runtime errors - the default value of 0 will be returned.
由于您的数组声明是原始类型,您不会收到任何编译器或运行时错误 - 将返回默认值 0。
If your array had been an array of Objects, then the array would hold nullfor any element not specifically assigned.
如果您的数组是一个对象数组,则该数组将保存null任何未专门分配的元素。
回答by R. Martinho Fernandes
I won't return nullbecause intis a primitive type. It will return the default intvalue, which is 0.
我不会返回,null因为int它是原始类型。它将返回默认int值,即0。
There is no way of knowing if any element has been set, short of keeping a separate boolean array.
除了保留一个单独的布尔数组之外,无法知道是否设置了任何元素。
回答by jcrossley3
In Java, only reference variables are initialized to null. Primitives are guaranteed to return appropriate default values. For ints, this value is 0.
在 Java 中,只有引用变量被初始化为 null。原语保证返回适当的默认值。对于整数,此值为 0。
回答by unwind
No.
不。
Your array elements are only big enough to hold ints, in this case. There is no place to store the information about if the element "exists". It has been allocated, thus it exists. In Java, newly allocated intarrays will be initialized to all elements zero.
int在这种情况下,您的数组元素仅足以容纳s。没有地方存储有关元素是否“存在”的信息。它已被分配,因此它存在。在 Java 中,新分配的int数组将被初始化为所有元素为零。
回答by TofuBeer
You can use checkstyle, pmd, and findbugson your source (findbugs on the binary) and they will tell you things like this.
您可以在源代码上使用checkstyle、pmd和findbugs(二进制文件上的 findbugs),它们会告诉您类似的信息。
Unfortunately it doesn't look like they catch this particular problem (which makes sense the the array is guaranteed to have each member set to 0, null, or false).
不幸的是,他们似乎没有发现这个特定的问题(这是有道理的,数组保证将每个成员设置为 0、null 或 false)。
The use of those tools can will catch instance and class members (that are not arrays) that are being used before being given a value though (similar sort of issue).
使用这些工具可以捕获在被赋予值之前正在使用的实例和类成员(不是数组)(类似的问题)。
回答by Dimitrios K.
Bit of a dead topic but I found it by accident so I'm gonna place my piece of wisdom (or whatever :P). You could use Arrays.Fill to fill your array when it's created with an invalid value like -1. Then you can easily do an if x<0 to check if YOU have initialized that specific array position or not. Just an idea...
有点死的话题,但我偶然发现了它,所以我要放置我的智慧(或其他任何东西:P)。您可以使用 Arrays.Fill 在使用无效值(如 -1)创建数组时填充数组。然后你可以很容易地做一个 if x<0 来检查你是否已经初始化了那个特定的数组位置。只是一个想法...

