Can I iterate over an array defined within a for-loop in Java? -
does java offer way following without resorting declaring array in separate variable?
for (string s : {"hey", "there"}) { // ... }
the best can come is:
for (string s : new arraylist<string>() {{ add("hey"); add("there"); }}) { // ... }
which isn't pretty.
well, least can this:
for (string s : new string[]{"hey", "there"}) { // ... }
since array
s "iterable" in java (although not implementing iterable
), iterate on array
instead of arraylist
, in-line initialized.
Comments
Post a Comment