Home > OS >  Python backwards for loop converted into java, not working
Python backwards for loop converted into java, not working

Time:02-01

I am so triggered by this. I am rewiriting a piece of python code into java. And my program has not been working. I have just found out why. It is the backwards for loop!

Python

for i in range(len(array) - 1, 2, -2): 

MY written in java

for (int i = diff.size() - 1; i < 2; i = i - 2) {}

I've been pulling my hair out. Does anyone know what is wrong??!!!

CodePudding user response:

The Java version terminates when i >= 2, while the Python version terminates i <= 2. To fix, the loop condition should be i > 2 for the Java version.

  •  Tags:  
  • Related