R
Red Orchid
The reference document :
http://java.sun.com/j2se/1.5.0/docs/guide/language/foreach.html
The above document says :
"the for-each construct combines beautifully
with generics. It preserves ..............
..... for nested iteration! Feast your eyes:"
I can 'feast' my eyes on the following 'beutifull'
code. But it is an error.
///////////////////////////////////////
void swap(int i, int j) {
int t;
t = i;
i = j;
j = t;
}
....
int[] aArr = new int[]{7, 1, 2};
int[] bArr = new int[]{6, 1, 5};
for (int a : aArr)
for (int b : bArr)
if (a < b) {
swap(a, b);
}
/////////////////////////////////////////
The for-each loop seems to be very prone to error.
Can you feel confident that you will use the for-each
loop correctly in a large code?
Of cource, the above document says :
" .. These shortcomings were known by the designers,
who made a conscious decision to go with a clean,
simple construct that would cover the great majority
of cases."
a clean, simple construct ??
What do you think about the for-each loop ?
Thanks.
http://java.sun.com/j2se/1.5.0/docs/guide/language/foreach.html
The above document says :
"the for-each construct combines beautifully
with generics. It preserves ..............
..... for nested iteration! Feast your eyes:"
I can 'feast' my eyes on the following 'beutifull'
code. But it is an error.
///////////////////////////////////////
void swap(int i, int j) {
int t;
t = i;
i = j;
j = t;
}
....
int[] aArr = new int[]{7, 1, 2};
int[] bArr = new int[]{6, 1, 5};
for (int a : aArr)
for (int b : bArr)
if (a < b) {
swap(a, b);
}
/////////////////////////////////////////
The for-each loop seems to be very prone to error.
Can you feel confident that you will use the for-each
loop correctly in a large code?
Of cource, the above document says :
" .. These shortcomings were known by the designers,
who made a conscious decision to go with a clean,
simple construct that would cover the great majority
of cases."
a clean, simple construct ??
What do you think about the for-each loop ?
Thanks.