I have read that
for (int i1 : A) {
}
is better than
for (int i1=0; i1 < A.length; i++) {
}
but if I want to access index value in first one is there any mean or should I use the second one only.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Wikipedia states:
If you want to use index, it’s better to use latter version.
Yes, you can
but again, not recommended. if you need index in Enahanced for loop, rethink your logic.
Java recommends the enahanced for loop: Source (See last line on page)