简体   繁体   English

如何查找Java中的数组列表中特定元素出现的次数

[英]How to find the number of times a specific element occurs in an Array list in Java

I have written a method with the aim to count the number of times a specific flavour of crisps appears in a snack machine in blueJ 我写了一种方法,目的是计算特定口味的薯片在blueJ小吃机中出现的次数

public int countPacks(String flavour){

        int n = 0;
        int nrFlavour = 0;
        while( n < packets.size()) {
            if( packets.get(n).equals(flavour)){
                nrFlavour++;
                n++;
            }
            else n++;
        }
        return nrFlavour;
}

I have an Arraylist 'packets' which holds PackOfCrisps objects which have a specific flavour. 我有一个Arraylist'packets',其中包含具有特定风味的PackOfCrisps对象。 However when I have added say three packets of "salt" flavour crisps and I run this method, inputting "salt" as the flavour, it just returns 0 as though there are no PackOfCrisps objects with flavour "salt". 但是,当我添加了说三个数据包的“盐”风味薯片并运行此方法时,输入“ salt”作为风味剂,它只会返回0,就好像没有带有“盐”风味的PackOfCrisps对象一样。 Sorry if this doesn't make sense. 抱歉,如果这没有意义。 I am very new to Java and I have tried to explain my problem the best I can. 我对Java还是很陌生,我已经尽力解释了我的问题。 :) :)

The list packets holds PackOfCrisps objects, and the method takes a String parameter. 列表packets包含PackOfCrisps对象,并且该方法采用String参数。 So the statement packets.get(n).equals(flavour) is comparing a PackOfCrisps object to a String , hence the count variable will never increase. 因此,语句packets.get(n).equals(flavour)PackOfCrisps对象与String进行比较,因此count变量将永远不会增加。

You need to compare the flavour string to the specific field of the object, something like: 您需要将flavour字符串与对象的特定字段进行比较,例如:

if(packets.get(n).getFlavour().equals(flavour)){

On a side note, you can replace the while loop with a simple for loop and remove the increment of n . 附带一提,您可以使用简单的for循环替换while循环,并删除n的增量。

There is a built-in solution to your problem, you can use this method 您的问题有内置的解决方案,您可以使用此方法

You can rewrite your countPacks method like this : 您可以像这样重写您的countPacks方法:

public int countPacks(String flavour){
    return Collections.frequency(packets, flavour);
}

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

相关问题 查找元素在Java中出现的次数 - Finding the number of times an element occurs in Java 计算数组中出现int的次数(Java) - Counting number of times an int occurs in an array (Java) 您如何在数组/字符串中找到特定值以及在数组/字符串中出现了多少次 - How do you find a specific value in an array/string and how many times it occurs in the array/string 计算2D数组在给定列中元素出现的次数? - Counting the number of times an element occurs in a given column for a 2D array? 在Java中2d数组的每一列中出现值的次数? - Number of times a value occurs in each column of a 2d-array in Java? 如何在java中查找数组中的元素数量? - How to find number of element in an Array in java? 如何找到一个数字在Java数组中重复多少次 - How to find how many times a number is repeated in an array in java 如何在数组中找到并打印在数组中恰好出现 K 次的最小数字,其中 K 是用户输入? - How can i find and print the smallest number in array which occurs exactly K times in an array, where K is an user input? 一个数字出现多少次 - How many times a number occurs 如何在java中的字母数字数组列表中找到唯一字符串元素的最大数量 - How to find the max number of an unique string element in a alphanumeric Array list in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM