我可以使用Python的for循环遍历数组作为Java中是否有FOR ... IN ...循环与Python类似?
arr = []
for e in arr:
do smth. with each element
请问Java有相似的循环?如果我会分裂这样Java中的字符串:
temp = str.split(delimiter);
请问我有阵列可与“为... ...中的”类似的语句来循环?
我可以使用Python的for循环遍历数组作为Java中是否有FOR ... IN ...循环与Python类似?
arr = []
for e in arr:
do smth. with each element
请问Java有相似的循环?如果我会分裂这样Java中的字符串:
temp = str.split(delimiter);
请问我有阵列可与“为... ...中的”类似的语句来循环?
还有就是foreach
“声明”:
for (final E element: array) {
// do whatever with element
}
它与数组和各种收藏以及(集合,列表,地图),一般用于任何X
类,它实现Iterable<X>
,即Iterable
本身(这意味着你可以写一个类在foreach语句中使用,这很好)。
所以,你可以直接写:
for (final String s: str.split(delimiter)) {
// work with s
}
为.split()
返回一个数组,这与foreach语句直接可用。也就是说,如果你不需要另一个变量来保存数组索引,无论出于何种原因。
for (String part: str.split(delimiter)){
// ....
}
for循环在java中。
http://www.roseindia.net/java/beginners/ForLoop.shtml
Java 1.5中,使用增强的for循环。
for (String str : temp) {
System.out.println(str);
}
你有试过什么吗?或许阅读任何帮助?在我最喜欢的搜索引擎中搜索2秒钟后发现http://docs.oracle.com/javase/1.5.0/docs/guide/language/foreach.html – ManseUK 2012-01-06 11:34:47
http://docs.oracle.com/javase/tutorial/java /nutsandbolts/for.html – 2012-01-06 11:38:56