Created
March 25, 2015 17:29
-
-
Save ecornell/7690cfb74db4b7801b23 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
/** | |
* Created by ecornell on 3/25/2015. | |
*/ | |
public class Loops { | |
public static void main(String[] args) { | |
String[] list = {"A", "B", "C", "D", "E"}; | |
// FOR | |
System.out.println("-- FOR --"); | |
for (int i = 0; i < list.length; i++) { | |
System.out.println(list[i]); | |
} | |
// DUAL FOR | |
System.out.println("\n -- DUEL FOR --"); | |
for (int i = 0, j = list.length - 1; i < list.length; i++, j--) { | |
System.out.println(list[i] + " " + list[j]); | |
} | |
// FOR EACH | |
System.out.println("\n -- FOR EACH --"); | |
for (String s : list) { | |
System.out.println(s); | |
} | |
// ITERATOR | |
System.out.println("\n -- ITERATOR --"); | |
List<String> aList = Arrays.asList(list); | |
for (Iterator iter = aList.iterator(); iter.hasNext();) { | |
String key = (String) iter.next(); | |
System.out.println(key); | |
} | |
// WHILE | |
System.out.println("\n -- WHILE --"); | |
int k = 0; | |
while (k < list.length) { | |
System.out.println(list[k++]); | |
} | |
// DO/WHILE | |
System.out.println("\n -- DO/WHILE --"); | |
int l = 0; | |
do { | |
System.out.println(list[l++]); | |
} while (l < list.length); | |
} | |
} | |
// Output | |
// | |
// -- FOR -- | |
// A | |
// B | |
// C | |
// D | |
// E | |
// | |
// -- DUEL FOR -- | |
// A E | |
// B D | |
// C C | |
// D B | |
// E A | |
// | |
// -- FOR EACH -- | |
// A | |
// B | |
// C | |
// D | |
// E | |
// | |
// -- ITERATOR -- | |
// A | |
// B | |
// C | |
// D | |
// E | |
// | |
// -- WHILE -- | |
// A | |
// B | |
// C | |
// D | |
// E | |
// | |
// -- DO/WHILE -- | |
// A | |
// B | |
// C | |
// D | |
// E |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment