Just curious about the following code:
If I use a for each of loop on an arraylist to call this method three times, each time with a different object, that has a different location, and I use it to set the locations for each object, will it create a new loc arraylist for each object or just set them all into one arraylist?
Okay so I got this code from Head First Java, in the section where they make the dotcom battle ships game - the more advanced version, and I'm trying to understand how it works.
I'll post the relevant code (at least what I think is relevant with very little experience in programming) and hopefully someone can help me understand?
Now the thing I'm confused about with this code is that there needs to be a different arrayList for each dotcom in the check yourself method, because if a dotcom is sunk it needs to print "kill", however if there all stored in one ArrayList, it will only print "kill" when ALL dotcoms are sunk from my understanding. The dotComs unique location arraylist needs to be used in the "checkyourself" method, which currently uses the locationCells arraylist I believe. I'm just wondering, does this code use a unique arraylist for each dotcom object in the check yourself method, and if not, how would it work to print "kill" when EACH dotCom is sunk?
P.S.: This forum is great, you're all so helpful and quick to respond!
Each dotComsList has its own ArrayList of locationCells to check. For example, if the first dotComsList has d1, d2, and d3 those would be stored in the locationCells list for that object. Then for the second dotComsList maybe it has a1, a2, a3 in the locationCells list.
Next when the checkUserGuess method runs, it loops through each dotComsList object and checks the locationCells list within it. So it will start with the first dotComsList object and then run checkYourself on that. When it runs checkYourself, the code looks in the locationCells list which in my example contains d1, d2, and d3. The code looks for the userInput within the locationCells list and if it exists in the list then the item is removed and either 'kill' or 'hit' is returned. When the last value is found/removed from the list 'kill' is returned.
After it finishes with the first dotComsList, it looks at the next dotComsList (a1, a2, and a3). And this continues through the same process looking for values in the locationCells.
Here's another way to explain it visually.