简体   繁体   中英

Java: ArrayList cannot find symbol

I am working on a Robot Maze where the robot finds the target without crashing into walls. I know I've missed something or done something incorrectly (most likely a couple of things haha) but I've been racking my brain over it for a couple of hours now and tried several alternatives. I'm pretty sure my error is either where and how I declared the ArrayList .

Any help is appreciated :)

I would post a picture of the compiling error messages but I do not have 10 reputation points. Essentially, there are 10 error messages saying it cannot find the symbol for the passageDirections and nonwallDirections .

PS: I am a beginner programmer, still learning so explain your answer as if you were to explain it to a three year old :)

i

Your passageDirections variable is local to your passageExits method. You can't access it from other methods.

If you want to access it from all methods of your class, make it an instance variable.

The same applies to nonwallDirections which is local to nonwallExits method.

public class Explorer2 {
    ...
    private ArrayList<Integer> nonwallDirections = new ArrayList<Integer>();
    private ArrayList<Integer> passageDirections = new ArrayList<Integer>();
    ...
}

You are tyring to access passageDirections and passageExists as a variable but they are methods. You have to create an private field variable such as

private int passageDirections;

and update the field in the passageDirections method in order to access them in the way you are trying to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM