简体   繁体   English

您如何在数组/字符串中找到特定值以及在数组/字符串中出现了多少次

[英]How do you find a specific value in an array/string and how many times it occurs in the array/string

I am having difficulty trying to code a part of a program in which I check to see if certain input is in an array/string (I can convert the array to an string). 我在尝试编码程序的一部分时遇到困难,在该程序中,我检查是否某些输入在数组/字符串中(我可以将数组转换为字符串)。 It also does not stop their, i need to figure out how many times the value I entered is in that array. 这也不会阻止他们,我需要弄清楚我输入的值在该数组中的次数。 For example: I have an array of 2 2 3 1 2. I enter in 2 and I want my program to count the number of twos I have in the array and store it to an integer value. 例如:我有一个2 2 3 1 2的数组。我输入2,我想让我的程序计算数组中的二进制数并将其存储为整数值。 Here is the code I have thus far, but it does not seem to work, when I try to store it to a space in a 2d array. 这是我到目前为止的代码,但是当我尝试将其存储到2d数组中的空间时,它似乎不起作用。

if (sv1==1)
{
    int onesValue=0; 
    int answer; 
    v1=p1.charAt(0);
    v2=p1.charAt(1);
    v3=p1.charAt(2);
    v4=p1.charAt(3);
    v5=p1.charAt(4);

    if (v1==1)
    {
        onesValue++; 
    }
    if (v2==1)
    {
        onesValue++;
    }
    if (v3==1)
    {
        onesValue++;

    }
    if (v4==1)
    {
        onesValue++;
    }
    if (v4==1)
    {
        onesValue++;
    }
    if (v5==1)
    {
        onesValue++;
    }

    scoreSheet[0][0] =onesValue; 
}

sv1, is a string that was converted to a double, p1 is a string converted to an array If anyone could help me with this it would be greatly appreciated. sv1是转换成双精度型的字符串,p1是转换成数组的字符串。如果有人可以帮助我,将不胜感激。

To my understand I think this should count the occurrence of required character . 据我了解,我认为这应该算是必需品的出现。

    String find="2";
    String[] array = {"2", "2", "3", "1",  "2"};
    Arrays.stream(array)
            .collect(Collectors.groupingBy(s -> s))
            .forEach((k, v) -> System.out.println(k.equals(find)?k + " - " + v.size()+" times":""));

Update: 更新:

If its an int array you can simply go with this: 如果它是一个int数组,则可以使用以下代码:

String find=2;
long count =  Arrays.stream(array).filter((s)-> s == find).count();
        System.out.println("count of "+find+ " is - "+count);

Will it help ? 有帮助吗?

        int i[] = {2,5,4,23,7,2,4,2};
        int occurnce = 0;
        Scanner s = new Scanner(System.in);
        System.out.println("Enter the number :");
        int number = s.nextInt();
       for (int n=0;n<i.length;n++){
           if(i[n] == number)
               occurnce++;
      }
       if(occurnce>0)
        System.out.println("Number of occurance : " +occurnce);
       else
        System.out.println("Number doesnt exist");  
 Integer arry[] = {2,5,4,23,7,2,4,2};

    Map<Integer, Integer> resultMap = new HashMap<Integer, Integer>();
    for (int index=0; index < arry.length;index++ ) {
        int counter = 1;
        Integer valueTobeChecked = arry[index];
        if (!resultMap.containsKey(valueTobeChecked)) {
            resultMap.put(valueTobeChecked, counter);
        } else {
            counter = resultMap.get(valueTobeChecked);
            resultMap.put(valueTobeChecked, ++counter);
        }
    }
    System.out.println(resultMap);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM