0

I'm currently catching an ArrayOutofBoundsException for this loop, but I don't see how, as I've already explored all other options to have created this loop. For reference, the lengths of iValues.length are 5, 3, 5, 7, and 5.

`for (int i = 1; i < iValues.length; i = i + 2) {

g.addEdge(iValues[0], iValues[i], iValues[i + 1]);

}`

Here's what I've tried: decreasing iValues.length, and incrementing i within the body of the for loop. None of this seems to work. I did the math and all of these should be running and then terminated right after.

  • You don't need to catch. Change the loop to use `i < iValues.length - 1` and don't use `i = i + 2` – OneCricketeer Nov 09 '21 at 19:22
  • Arrays are zero-indexed, i.e. an array of length `5` can be accessed with indices `0`, `1`, `2`, `3` and `4`. --- You increment the iteration variable in the `loop`-body as well as in the loop-header. I recommend reading a tutorial on `for`-loops, e.g. [this one from `oracle.com`](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html). – Turing85 Nov 09 '21 at 19:23

0 Answers0