What is the best way to iterate for-loop?

I need to repeat with for-loop, but I want to know which way is better.

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));
    }
}

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));
    }
}

In the above example, both for-loopshave the same content, but only the difference in the state of the loop. Can someone tell me what actually happens in the above iterations?

+4
source share
2 answers

You all misunderstand this. You focus on aspects that are almost irrelevant; and you do it, you wrote bad code) in both examples!

First of all, you are not using raw types .

Instead you go:

List<String> theWords = new ArrayList<>();

( : List . )

Java "" ,

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

; for-each /:

for (String aWord : theWords) 

ints ..

: Java C. ; ; . : " " - . : " " ( , " ", ), . JVM JIT ... , !

; " " ; JIT (, ).

+12
  • 1 , tmpList.size() .

  • 2 , .

Cleanest

- , IDE:

for ( String s : tmpList ) {
    System.out.println(s);
}

, getter:

String[] tmpList = String[24];
tmpList[0] = "me";
tmpList[1] = "you";
...

for ( int i = 0; i < tmpList.length; ++i ) {
    System.out.println(tmpList[i]);
}

- , , .

System.out.println("me");
System.out.println("you");
...

, KISS ( ) , .

+4

Source: https://habr.com/ru/post/1653390/


All Articles