use*_*856 3 java foreach loops
以下是否会产生不必要的内存使用
String[] words = text.split(" ");
for (String s : words)
{...}
Run Code Online (Sandbox Code Playgroud)
或者text.split(" ")每次循环重复时都会调用以下内容
for (String s : text.split(" "))
{...}
Run Code Online (Sandbox Code Playgroud)
哪种方式更可取?
There are pluses to each way of writing your loop:
for, and inspect wordswords into the namespace, so you can use the name elsewhere.As far as performance and readability go, both ways are equally good: the split will be called once before the start of the loop, so there are no performance or memory usage consequences to using the second code snippet.
| 归档时间: |
|
| 查看次数: |
225 次 |
| 最近记录: |