Java string iterator example
July 14, 2011 08:26:05 Last update: July 14, 2011 08:26:05
You can iterate through an array of String like this:
or like this:
For a string
or like this:
String[] a = { "one", "two", "three" }; for (int i = 0; i < a.length; i++) { System.out.println(a[i]); }
or like this:
for (String s: a) { System.out.println(s); }
For a string
Collection, iterate like this:
Collection<String> c = Arrays.asList("one", "two", "three"); Iterator it = c.iterator(); while (it.hasNext()) { System.out.println(it.next()); }
or like this:
Collection<String> c = Arrays.asList("one", "two", "three"); for (String s: c) { System.out.println(s); }