About Question enthuware.ocajp.i.v8.2.1454 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
fanfa86
Posts: 3
Joined: Mon Sep 04, 2017 1:21 pm
Contact:

About Question enthuware.ocajp.i.v8.2.1454 :

Post by fanfa86 »

Why enhanced for loop can not be used for task 3?

This could work:

Code: Select all

public class HelloWorld{

     public static void main(String []args){
        String[] array = {"0","1","2","3","4","5"};
        int i = 0;
        for(String v : array){
            if (i % 2 == 0){
                System.out.println(v);
            }
            i++;
        }
     }
}
$javac HelloWorld.java
$java -Xmx128M -Xms16M HelloWorld
0
2
4

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1454 :

Post by admin »

You are not using just the enhanced for loop in your code. You are using iteration variable as well. That is the whole point of this question. Enhanced for loop does not have an iteration variable.
If you like our products and services, please help us by posting your review here.

flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1454 :

Post by flex567 »

Code: Select all

You are using iteration variable as well.
There is no mention that you cant use iteration variable.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1454 :

Post by admin »

That is the whole point of this question. Enhanced for loop does not have an iteration variable. Otherwise, all loops are interchangeable. What you can do with one loop, you can do with another.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 38 guests