Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java-使用binarySearch查找数组中某些元素出现的次数,而不使用If_Java_Arrays_Search - Fatal编程技术网

Java-使用binarySearch查找数组中某些元素出现的次数,而不使用If

Java-使用binarySearch查找数组中某些元素出现的次数,而不使用If,java,arrays,search,Java,Arrays,Search,我试图在int数组中查找整数的出现次数,但不使用if语句 long count = Arrays.stream(list).filter(x -> x == number).count(); 我理解,通常情况下,可以使用for或foreach简单地遍历数组,并在每次元素匹配条件时使用if增加一个计数变量 但是,由于我无法对数组进行排序,因此我正在考虑对数组进行排序,找出数组中每组重复元素的第一个索引和最后一个索引之间的差异,并尝试利用binarySearch获取索引,如下所示: int[

我试图在int数组中查找整数的出现次数,但不使用if语句

long count = Arrays.stream(list).filter(x -> x == number).count();
我理解,通常情况下,可以使用for或foreach简单地遍历数组,并在每次元素匹配条件时使用if增加一个计数变量

但是,由于我无法对数组进行排序,因此我正在考虑对数组进行排序,找出数组中每组重复元素的第一个索引和最后一个索引之间的差异,并尝试利用binarySearch获取索引,如下所示:

int[] list = {9, 9, 7, 5, 9, 9, 3, 1, 1};
Arrays.sort(list);

// list is now {1, 1, 3, 5, 7, 9, 9, 9, 9} for binarySearch

/* Finding the difference between the indices of the first and last occurrences of
   9, for example, could yield the number of occurrences. */

int firstIndex = Arrays.binarySearch(list, 0);
// int lastIndex = ??

然而,我对如何找到上次事件的索引感到困惑。另外,binarySearch是检索数组中特定键的索引的唯一方法。有人能告诉我吗?

最后一次出现的索引是下一个值(即
值+1
)的第一次出现的索引减去1

确保处理下一个值存在和不存在的情况


但是,我怀疑您不打算在这里使用库API,您应该在另一个数组中遍历数组,计算所有值的出现次数,然后返回您要查找的数字的出现次数值。如果需要,则无

最后一次出现的索引是下一个值(即
值+1
)的第一次出现的索引减去1

确保处理下一个值存在和不存在的情况


但是,我怀疑您不打算在这里使用库API,您应该在另一个数组中遍历数组,计算所有值的出现次数,然后返回您要查找的数字的出现次数值。如果需要,则无

我不确定这有什么规则。这是作弊吗

static int count(int number, int[] list) {
    int count = 0;
    for (int a : list)
        switch(a == number ? 1 : 0) {
            case 1: count++; 
        }
    return count;
}

我不知道这有什么规则。这是作弊吗

static int count(int number, int[] list) {
    int count = 0;
    for (int a : list)
        switch(a == number ? 1 : 0) {
            case 1: count++; 
        }
    return count;
}

这个怎么样?我认为它没有使用任何
if
语句

long count = Arrays.stream(list).filter(x -> x == number).count();

这个怎么样?我认为它没有使用任何
if
语句

long count = Arrays.stream(list).filter(x -> x == number).count();

或者只是
count+=(a==数字)?1:0
。不需要
开关
。您使用的是if语句“伪装”:)还是只使用
count+=(a==number)?1:0
。无需使用
开关
。您正在使用if语句“伪装”:)另一方面,在您的情况下,使用
Arrays.binarySearch()
方法不适合实现:Open声明
int java.util.Arrays.binarySearch(int[]a,int-key)
使用二进制搜索算法在指定的整数数组中搜索指定的值。在进行此调用之前,必须对数组进行排序(如通过sort(int[])方法)。如果未排序,则结果未定义。如果数组包含多个具有指定值的元素,则无法保证将找到哪个元素。另一方面,在您的情况下,使用
Arrays.binarySearch()
方法不适合实现:Open Declaration
int java.util.Arrays.binarySearch(int[]a,int-key)
使用二进制搜索算法在指定的整数数组中搜索指定的值。在进行此调用之前,必须对数组进行排序(如通过sort(int[])方法)。如果未排序,则结果未定义。如果数组包含多个具有指定值的元素,则无法保证找到哪个元素。