I am working on a Robot Maze where the robot finds the target without crashing into walls. I have commented my code thoroughly so hopefully it'll be understandable.
At the dead end and corridor, there will be only one passage in the passageDirections ArrayList. How do I return this one and only direction?
Any help is appreciated :)
PS: I am a beginner programmer, still learning so explain your answer as if you were to explain it to a three year old :)
You can use passageDirections.get(0), which will return the first element in the list.
Since (I hope that) you guarantee there is a single element in the list, that solves your issue.
https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#get(int)
You should check the size before calling to prevent IndexOutOfBoundsException
if (passageDirections.size() > 0)
passageDirections.get(0)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With