迭代for循环的哪种方式更好?

Gab*_*abu 4 java for-loop arraylist

我必须迭代一次for-loop,但我想知道哪种方式更好.

1:

import java.util.ArrayList;

public class FindTime {
    public static void main(String[] args) {
        ArrayList tmpList = new ArrayList();
        tmpList.add("me");  tmpList.add("you ");
        tmpList.add("I");   tmpList.add("Us");
        tmpList.add("me");  tmpList.add("you ");
        tmpList.add("I");   tmpList.add("Us");
        tmpList.add("me");  tmpList.add("you ");
        tmpList.add("I");   tmpList.add("Us");
        tmpList.add("me");  tmpList.add("you ");
        tmpList.add("I");   tmpList.add("Us");
        tmpList.add("me");  tmpList.add("you ");
        tmpList.add("I");   tmpList.add("Us");
        tmpList.add("me");  tmpList.add("you ");
        tmpList.add("I");   tmpList.add("Us");

        long startTime = System.nanoTime();
        for (int i = 0,size=tmpList.size(); i < size; i++) {
            System.out.println(tmpList.get(i).toString());
        }
        long endTime = System.nanoTime();

        System.out.println("Time Duration ::->" + (endTime - startTime));
    }
}
Run Code Online (Sandbox Code Playgroud)

2:

import java.util.ArrayList;

public class FindTime {
    public static void main(String[] args) {
        ArrayList tmpList = new ArrayList();
        tmpList.add("me");  tmpList.add("you ");
        tmpList.add("I");   tmpList.add("Us");
        tmpList.add("me");  tmpList.add("you ");
        tmpList.add("I");   tmpList.add("Us");
        tmpList.add("me");  tmpList.add("you ");
        tmpList.add("I");   tmpList.add("Us");
        tmpList.add("me");  tmpList.add("you ");
        tmpList.add("I");   tmpList.add("Us");
        tmpList.add("me");  tmpList.add("you ");
        tmpList.add("I");   tmpList.add("Us");
        tmpList.add("me");  tmpList.add("you ");
        tmpList.add("I");   tmpList.add("Us");

        long startTime = System.nanoTime();
        for (int i = 0;i < tmpList.size(); i++) {
            System.out.println(tmpList.get(i).toString());
        }
        long endTime = System.nanoTime();

        System.out.println("Time Duration ::->" + (endTime - startTime));
    }
}
Run Code Online (Sandbox Code Playgroud)

在上文中,两者for-loops具有相同的内容,但仅具有循环条件的差异.谁能告诉我上面的迭代实际发生了什么?

Gho*_*ica 13

你错了.你专注于几乎无所谓的方面; 这样做,你写了不好的代码 - 在你的例子中!

首先,您不使用原始类型.

你改为:

List<String> theWords = new ArrayList<>();
Run Code Online (Sandbox Code Playgroud)

(请注意:使用List作为列表对象的实际类型也是更好的做法.无需将其背后的实现暴露给该列表的用户)

Java有很好的"填充"列表的方法,比如

List<String> theWords = Arrays.asList("me", "didn't", "know", "that", "but", "could", "have");
Run Code Online (Sandbox Code Playgroud)

然后; 你使用for-each来迭代各种集合/数组:

for (String aWord : theWords) 
Run Code Online (Sandbox Code Playgroud)

并且你不再担心所有这些低级别的循环,包括整数和增量.

换句话说:Java不是C.我们在许多地方都有更好的抽象; 你最好关注那些; 因为他们照顾这些微妙之处.含义:专注于编写"更高级别"代码 - 因为这会创建可读代码.请放心:如果您遵循典型的"最佳实践"(如上所述使用'for each'进行迭代循环) - 您无需担心性能问题.因为JVM和JIT最擅长优化...如果你使用它们提供给你的抽象!

努力做到聪明; 表达"低级别"的东西甚至可能产生负面影响; 因为它可能会阻止JIT进行其荣耀优化工作(可能不是在这种情况下).

  • 如果我可以"专注于编写"更高级别的代码,我会支持1000次,因为这会创建可读代码 (2认同)