如何在 Java 中编写“for x in list”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14531867/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 16:33:56  来源:igfitidea点击:

How to write "for x in list" in Java?

javalistsyntaxfor-loopforeach

提问by Jon Anderson

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

我想知道如何在 Java 中表达某种类型的 for 循环。

In Python I would use:

在 Python 中,我会使用:

for x in lst1:
       return 10

How would I do this in Java?

我将如何在 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

我只想知道如何做其他类型的循环

回答by Eric

int[] myints = {1, 2, 3, 4, 5};
for(int i : myints) {
    System.out.println(i);
}

回答by Josh Miller

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 listthat you and other's will most likely benefit from watching.

那是——据我所知——你的python代码的精确转换。我最近做了一个关于遍历列表视频教程,您和其他人最有可能从观看列表中受益。

Good luck!

祝你好运!