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

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

Accessing sub-array and counting an element in arrays in Java

javaarrays

提问by Amen

I have 2 questions about arraysin javaand any help would be appreciated.

我有2个问题阵列java任何帮助将不胜感激。

  1. Is there a way to accessa sub-array(something like this on python: array[1:5])

  2. I want to know does javahas a function that countsa specific element in an arrayor not.

  1. 有没有办法进入一个子阵列(像这样的python: array[1:5]

  2. 我想知道确实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 copyOfRangestatic method of Arraysclass.

对于您的第一个问题,您可以使用类的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.asListto obtain a List<E>, and then use the subListmethod 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 的做法是:

  1. Arrays.stream(array, 1, 5)
  2. Arrays.stream(array).filter(x -> x == element).count()
  1. Arrays.stream(array, 1, 5)
  2. Arrays.stream(array).filter(x -> x == element).count()

where arrayis your input int[]and elementis the integer item to be counted

这里array是你的输入int[]element量进行统计的整数项目

Explanation:

解释:

  1. Arrays.stream(array, 1, 5)will return you a Streamobject containing elements present in the arrayfrom index 1 to 4. That Streamcan be further converted to int[]if needed using toArray()or can be directly used if some aggregate function is required on it like min()
  2. Arrays.stream(array).filter(x -> x == element).count()will first convert the given arrayto Streamand then will apply the filter()filtering outany element that doesn't match the boolean condition specified and then will return the count()of the filtered inelement.
  1. Arrays.stream(array, 1, 5)将返回一个Stream包含array从索引 1 到 4 中存在的元素的对象。如果需要,Stream可以进一步转换为int[]使用,toArray()或者如果需要某些聚合函数,则可以直接使用它,例如min()
  2. Arrays.stream(array).filter(x -> x == element).count()将首先将给定的转换arrayStream,然后将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.

注意:用于计算数组中某项出现次数的此方法或任何其他方法必须在内部循环遍历元素。