java - Which way to iterate for-loop is better? -


i have iterate for-loop, want know way 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();         (int = 0,size=tmplist.size(); < 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();         (int = 0;i < tmplist.size(); i++) {             system.out.println(tmplist.get(i).tostring());         }         long endtime = system.nanotime();          system.out.println("time duration ::->" + (endtime - starttime));     } } 

in above, both for-loops have same content difference in loop condition. can tell me happening in above iterations?

you getting wrong. focusing on aspects don't matter; , doing so, wrote bad code - in both examples!

first of all, don't use raw types.

you go instead:

list<string> thewords = new arraylist<>(); 

( please note: better practice use list actual type of list object. no need expose implementation behind users of list )

and java has nice ways of "filling" list, like

list<string> thewords = arrays.aslist("me", "didn't", "know", "that", "but", "could", "have"); 

then; use for-each iterate all kinds of collections/arrays:

for (string aword : thewords)  

and stop worrying these low level loops ints , increments , stuff.

in other words: java not c. have better abstractions in many places; better focus on those; take care of such subtleties. meaning: focus on writing "higher level" code - because creates readable code. , assured: if follow typical "best practices" (as iterating loops using 'for each' above) - don't need worry performance. because jvm , jit best @ optimizing things ... if use abstractions offer you!

trying smart; , expressing things "low level" can have negative effects; because might prevent jit doing glory optimization work (probably not in case).


Comments