Felicia Felicia - 10 months ago 58
Java Question

Python For Loop Syntax to Java

for j in [c for c in coinValueList if c <= cents]:

How would you write this for loop out in java?
Is it

for(j=0, j <= cents, j++){
for(c=0; c<= cents, j++){

I'm not sure what c and j are supposed to be compared to.
CoinValueList = {1,5,10,25}
cents = 0 -- it's in its own for loop before these two.

Answer Source

Let's decompose:

array = [c for c in coinValueList if c <= cents] # produces an array of coins from coinValueList that are <= cents
for j in array: # iterates over array

So we can do that in only one loop, and the java equivalent would be:

for(int j=0; j<coinValueList.length; j++) {
    if(coinValueList[j] <= cents) {