Java中访问子数组并计算数组中的元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20800523/
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
Accessing sub-array and counting an element in arrays in Java
提问by Amen
I have 2 questions about arraysin java
and any help would be appreciated.
我有2个问题阵列中java
任何帮助将不胜感激。
Is there a way to accessa sub-array(something like this on
python: array[1:5]
)I want to know does
java
has a function that countsa specific element in anarray
or not.
有没有办法进入一个子阵列(像这样的
python: array[1:5]
)我想知道确实
java
有一个函数可以计算an 中的特定元素array
。
I don't want to use for loops.
我不想使用 for 循环。
采纳答案by Mehmet Sedat Güng?r
For your first question, you can use copyOfRange
static method of Arrays
class.
对于您的第一个问题,您可以使用类的copyOfRange
静态方法Arrays
。
Example use:
使用示例:
int[] array = new int[]{1, 2, 3, 4, 5};
int[] subArray = Arrays.copyOfRange(array, 1, 3);
//subArray = [2, 3]
And for your second question, that method has to traverse whole array and that cannot be done without a loop.
对于您的第二个问题,该方法必须遍历整个数组,并且没有循环就无法完成。
回答by Jon Skeet
For accessing part of an array, you could use Arrays.asList
to obtain a List<E>
, and then use the subList
method to obtain a slice:
要访问数组的一部分,您可以使用Arrays.asList
获取 a List<E>
,然后使用该subList
方法获取切片:
import java.util.*;
public class Test {
public static void main(String[] args) {
String[] array = { "zero", "one", "two", "three", "four", "five", "six" };
List<String> list = Arrays.asList(array);
List<String> sublist = list.subList(1, 5);
for (String item : sublist) {
System.out.println(item);
}
}
}
Output:
输出:
one
two
three
four
For your second question: even if such a method existed, it would have to loop. I'm not aware of any such method in the standard libraries, so I suggest you write your own using a loop.
对于您的第二个问题:即使存在这样的方法,它也必须循环。我不知道标准库中有任何此类方法,因此我建议您使用循环编写自己的方法。
回答by Vikas Prasad
The Java 8 way of doing this will be:
Java 8 的做法是:
Arrays.stream(array, 1, 5)
Arrays.stream(array).filter(x -> x == element).count()
Arrays.stream(array, 1, 5)
Arrays.stream(array).filter(x -> x == element).count()
where array
is your input int[]
and element
is the integer item to be counted
这里array
是你的输入int[]
和element
量进行统计的整数项目
Explanation:
解释:
Arrays.stream(array, 1, 5)
will return you aStream
object containing elements present in thearray
from index 1 to 4. ThatStream
can be further converted toint[]
if needed usingtoArray()
or can be directly used if some aggregate function is required on it likemin()
Arrays.stream(array).filter(x -> x == element).count()
will first convert the givenarray
toStream
and then will apply thefilter()
filtering outany element that doesn't match the boolean condition specified and then will return thecount()
of the filtered inelement.
Arrays.stream(array, 1, 5)
将返回一个Stream
包含array
从索引 1 到 4 中存在的元素的对象。如果需要,Stream
可以进一步转换为int[]
使用,toArray()
或者如果需要某些聚合函数,则可以直接使用它,例如min()
Arrays.stream(array).filter(x -> x == element).count()
将首先将给定的转换array
为Stream
,然后将filter()
过滤掉与指定的布尔条件不匹配的任何元素,然后返回count()
过滤后的元素的 。
Note: This or any other method for counting the number of occurrences of an item in an array will internally have to loopthough the elements.
注意:用于计算数组中某项出现次数的此方法或任何其他方法必须在内部循环遍历元素。