1

I am creating an A Star search algorithm to solve a 8 puzzle board and I've got all the Board object classes listed within an ArrayList. My problem is I need to run methods within each of the Board objects to allow me to check if they have reached there goal, get board information and other functions similar to that.

Problem is I can't find a way after a few hours of internet searching that will solve this problem, I tried using an iterator to do the job which seems like the right direction but I couldn't get it to work but I don't have any experience with them.

Any help would be of great help.

    public class Solve8Puzzle {
        ArrayList startNode;
        ArrayList nodes;
        public Solve8Puzzle() {
            startNode = new ArrayList();
            nodes = new ArrayList();
        }
        public boolean checkGoalNodes() {
            while( currently selected node has next ) {
                run current node goal check
            }
        }
    }
4
  • 1
    What is stored in startNode and nodes? Commented Feb 22, 2012 at 11:31
  • Is this a homework assignment? If so, please tag it as such. Commented Feb 22, 2012 at 11:31
  • Can you post the code you for using an Iterator? Did it fail to compile? Commented Feb 22, 2012 at 11:31
  • @TheEliteGentleman The startNode ArrayList contains the original Board object and the node ArrayList contains any new nodes created throughout the algorithm. Commented Feb 22, 2012 at 11:48

1 Answer 1

4
List<StartNode> startNode = new ArrayList<StartNode>();

.......................


for (StarNode node : starNodes) {
    // do what you want with the node
}

Other possibility

for (Iterator<StarNode> it = starNodes.iterator(); it.hasNext(); ) {
    StarNode node = it.next();
    // do what you want with the node
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks that helped me a lot with how to use and correctly format ArrayList and using Iterators

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.