How do I know how many times I repeat a for-each door in Java?

Asked 2 years ago, Updated 2 years ago, 73 views

for(String s : stringArray) {
  doSomethingWith(s);
}

How do I know how many times I repeat this if I have this for-each syntax? for(inti=0;i<boundary;i++) The value of i is the number of iterations In a for-each statement

int i = 0;
for(String s : stringArray) {
  doSomethingWith(s);
  i++;
}

Is this the only way you can do it?

for-loop loops java foreach

2022-09-21 16:18

2 Answers

Yeah, that's the simplest way. The for-each statement does not have an internal counter.


2022-09-21 16:18

Use stringArray.length to see the actual number of for statements.


2022-09-21 16:18

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.