arraylist - How to find the number of times a specific element occurs in an Array list in Java -
I have computed several times to reveal the specific taste of crisps in a snack machine in the blues
Public int calculation (string taste) {int n = 0; Int nrFlavour = 0; While (n & lt; packets.size ()) {if (packets.get (n). Par (taste)) {nrFlavour ++; N ++; } Other n ++; } Return nrFlavour; }
I have a bibliography packet in which pack of crripes are objects, which have specific taste. However, when I said that "salt" is called three packets of flavor and I run this method, "salt" is input in the form of flavor, so it just gives 0 as if Square "salt" There are no pack offscaps objects with. Sorry, if that does not mean I'm new to Java and I can tell my problem best I can. :)
list holds packet
PackOfCrisps
Objects, and method takes a string
parameter then statement packets.get (n). Comparing par value (tastes)
a PackOfCrisps
object to string
, so the counting variable will never increase.
You need to compare the taste
string in the specific area of the object, something like this:
if (packets.get (N) .getFlavour (). Equal (taste)) { Note one side, you can change the while
loop with a simple
Remove the loop and the increase of n
.
Comments
Post a Comment