J2SE 5.0 Topic Reinforced For Cycle

xiaoxiao2021-03-06  19

Have you tired of writing those machine code every time you write for cycles, especially when you need to traverse an array or collection, such as:

Public void showall (collection c) {for (iTerator iter = c.iterator (); it.hasnext ();) {system.out.println (iter.next (). Tostring ());}} public void Showall String [] sa) {for (int i = 0; i

Public void showall (Collection c) {for (object obj: c) {system.out.println (obj.tostring ());}} public void showall (String "sa) {for (String str: sa) {system .out.println (str);}}

Such a code looks clearer and concise, isn't it? (Is it a bitach like C #?) Plus generics, we can even make the first method more beautiful: public void showall (Collection C) {for (String str: c) {system.out .println (str);}}

With generic and enhanced For loops, we don't have to worry about the expression and nest of those who are annoying for the annoying for cycling. The following examples: When you need to access the Iterator and the subscript in the cyclic body,, for example, when you need to remove or modify the elements in the collection or array.

More detailed information about enhanced for loops, reference

Here.

转载请注明原文地址:https://www.9cbs.com/read-39797.html

New Post(0)