1

As I'm teaching myself Java, I've been programming a Battleship like game, but I've run into a problem with my method for making sure that when I create a new ship I don't add it unless all of its location values are unique.

I'm doing something wrong, because my method isn't working and I'm getting duplicate values (or ships that have the same location).

I have two ArrayLists:

  1. The first list is an ArrayList of the Ship objects (a field on my game controller class)
  2. The second list is an ArrayList of the Locations in the form of a String (e.g. A5, B17, etc.) (this is a field on my Ship object)

My method for checking to see if a ship I'm about to add to the ArrayList of Ship objects has unique locations is as follows:

private boolean hasUniqueLocs(Ship ship) {
        for (Ship x : theShips) {
            for (String y : ship.shipLocation) {
                System.out.println("Checking to see if " + x.shipLocation + " ship contains this value: " + y);
                if (x.shipLocation.contains(y)) {
                    return false;
                }
            }
        }
    return true;
}

So "theShips" is my ArrayList of Ships field on the GameController (with my Main method). And "shipLocation" is my ArrayList field on the Ship object that holds the locations.

My println() statement never runs, so for some reason I'm never getting the for loops going?

Thank you for any guidance or insight you can provide here!

6
  • 2
    Probably need more debugging. Run this through a debugger and see if you reach hasUniqueLocs() or add a print statement at the beginning. Also - Maroun has a good point - if there aren't any locations you won't reach the print statement. Commented Jan 3, 2014 at 22:58
  • 4
    Did you check the size of theShips and shipLocation? Commented Jan 3, 2014 at 22:58
  • 1
    as mentioned, debug to check the size of your lists, if your loop is not being executed is probably because not elements are found to iterate. Commented Jan 3, 2014 at 23:00
  • It might make the problem simpler if you used a HashMap for look ups, that way you could simplify the method by just returning (map.get(location) == null) . Commented Jan 3, 2014 at 23:05
  • My println() statement never runs then YES, you need to debug and see when the method is ever called. The logic here does not look like a problem, but you've wasted some time focusing the question this way Commented Jan 3, 2014 at 23:13

1 Answer 1

1

Okay, I blew it. I'm still new to using the debugger and I should have gone there first, but I'm so used to having errors in my code that I assumed that was the problem and that there was something about arraylists that I wasn't understanding.

After debugging, I realized that I wasn't actually adding the ships until the end of the method that was calling this method - so, as you guys pointed out, there weren't any objects in the arraylists yet. It was very easy to see my problem once I had this direction.

Thanks everyone!

Sign up to request clarification or add additional context in comments.

Comments

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.