简体   繁体   中英

How to write “for x in list” in Java?

I was wondering how to express a certain type of for loop in Java.

In Python I would use:

for x in lst1:
       return 10

How would I do this in Java?

I know for the range for-loops, I use
for(int i=0; i<100; j++) {
   return "asdf"
}

I just want to know how to do the other type of loop

int[] myints = {1, 2, 3, 4, 5};
for(int i : myints) {
    System.out.println(i);
}
int[] lst1 = {1,2,4,5,6,7,8,9};    
for (int x : lst1){
    return 10;
}

That's--as far as I'm aware--the exact conversion of your python code. I recently did a video tutorial on iterating through a list that you and other's will most likely benefit from watching.

Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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