[英]Iterating through array - java
I was wondering if it was better to have a method for this and pass the Array
to that method or to write it out every time I want to check if a number is in the array
. 我想知道是否有更好的方法,并将
Array
传递给该方法,或者每当我想检查数字是否在array
时将其写出来。
For example: 例如:
public static boolean inArray(int[] array, int check) {
for (int i = 0; i < array.length; i++) {
if (array[i] == check)
return true;
}
return false;
}
Thanks for the help in advance! 我在这里先向您的帮助表示感谢!
Since atleast Java 1.5.0 (Java 5) the code can be cleaned up a bit. 从至少Java 1.5.0(Java 5)开始,代码可以被清理一下。
Array
s and anything that implements Iterator
(eg Collection
s) can be looped as such: Array
和任何实现Iterator
东西(例如Collection
s)可以循环使用:
public static boolean inArray(int[] array, int check) {
for (int o : array){
if (o == check) {
return true;
}
}
return false;
}
In Java 8 you can also do something like: 在Java 8中,您还可以执行以下操作:
// import java.util.stream.IntStream;
public static boolean inArray(int[] array, int check) {
return IntStream.of(array).anyMatch(val -> val == check);
}
Although converting to a stream for this is probably overkill. 尽管为此转换为流可能有点过分。
You should definitely encapsulate this logic into a method. 您绝对应该将此逻辑封装到方法中。
There is no benefit to repeating identical code multiple times. 多次重复相同的代码没有任何好处。
Also, if you place the logic in a method and it changes, you only need to modify your code in one place. 此外,如果将逻辑放在方法中并且更改,则只需在一个位置修改代码。
Whether or not you want to use a 3rd party library is an entirely different decision. 您是否想要使用第三方库是一个完全不同的决定。
If you are using an array (and purely an array), the lookup of "contains" is O(N)
, because worst case, you must iterate the entire array. 如果您正在使用数组(并且纯粹是数组),则“包含”的查找是
O(N)
,因为最坏的情况是,您必须迭代整个数组。 Now if the array is sorted you can use a binary search, which reduces the search time to log(N)
with the overhead of the sort. 现在,如果数组已排序,您可以使用二进制搜索,这会减少使用排序开销的
log(N)
的搜索时间。
If this is something that is invoked repeatedly, place it in a function: 如果这是重复调用的内容,请将其放在函数中:
private boolean inArray(int[] array, int value)
{
for (int i = 0; i < array.length; i++)
{
if (array[i] == value)
{
return true;
}
}
return false;
}
You can import the lib org.apache.commons.lang.ArrayUtils 您可以导入lib org.apache.commons.lang.ArrayUtils
There is a static method where you can pass in an int array and a value to check for. 有一个静态方法,您可以传入一个int数组和一个值来检查。
contains(int[] array, int valueToFind) Checks if the value is in the given array. contains(int [] array,int valueToFind)检查值是否在给定数组中。
ArrayUtils.contains(intArray, valueToFind);
Using java 8 Stream API could simplify your job. 使用java 8 Stream API可以简化您的工作。
public static boolean inArray(int[] array, int check) {
return Stream.of(array).anyMatch(i -> i == check);
}
It's just you have the overhead of creating a new Stream
from Array
, but this gives exposure to use other Stream
API. 只是你有从
Array
创建一个新Stream
的开销,但是这会暴露使用其他Stream
API。 In your case you may not want to create new method for one-line operation, unless you wish to use this as utility. 在您的情况下,您可能不希望为单行操作创建新方法,除非您希望将其用作实用程序。 Hope this helps!
希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.