• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with ArrayLists  RSS feed

 
Jonathan Carroll
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings everyone,

First time poster!

I have been learning Java over the past few weeks, primarily with the Head First Java 2nd edition book. In the chapter were I build a simple battleship game, or DotCom as the book calls it, I have ran into an issue that I need some clarification on. Following the book I wrote the simple battleship game but as explained there was an issue with the fact that the player could just enter the same number repeatedly to score hits and at 3 hits the game ends. To resolve this, the concept of ArrayLists is introduced and the book shows how to fix the issue mentioned previously by removing the correct guess from the ArrayList so that it can't be repeated.

However it did not go into any details on what things needed to change in other classes so now the program doesn't work. The specific issue in the code is linked below. The full code can be viewed here on GitHub.

The error message on Eclipse is:
The method setLocationCells(ArrayList<String>) in the type SimpleDotCom is not applicable for the arguments (int[])


If I understand correctly, the error comes from trying to pass an array as a parameter into a method that requires an ArrayList. The setLocationCells requires an ArrayList parameter. I tried just changing the locations array into an ArrayList<Integer> but that just threw even more errors. Can anyone give some advice on the matter? I would like to understand this and fix it before I move on to the next chapter. Thanks and just say if you need more information!

 
Carey Brown
Saloon Keeper
Posts: 3314
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need more context (code) to be able to help. Can you post the code for setLocationCells() ? Is your game based on a 2 dimensional grid? If it's 2 dimensional then why wouldn't you need X,Y (row,col) locations?
 
Jonathan Carroll
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:I need more context (code) to be able to help. Can you post the code for setLocationCells() ? Is your game based on a 2 dimensional grid? If it's 2 dimensional then why wouldn't you need X,Y (row,col) locations?


It is 1 dimensional the first time we make the game. So for example there would be a row of 7 cells and there will be a battleship that is 3 cells wide randomly in that. Making a 2D version is the next step in the book.

Here is the code you requested.

 
Carey Brown
Saloon Keeper
Posts: 3314
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your setLocations() method takes an ArrayList<String> but you are passing in an int array. You'll have to convert the int's to Strings before adding them to the ArrayList<String>. Alternatively you could refactor your code to use Integers instead of Strings. This would also entail converting you guesses to Integers.


-alternativley-
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jonathan Carroll wrote:The setLocationCells requires an ArrayList parameter...

Which sounds to me like a bad mistake to begin with, because unless there's some very specific reason it needs an ArrayList, it should just take a List (or possibly a List that also implements RandomAccess).

I tried just changing the locations array into an ArrayList<Integer> but that just threw even more errors.

And one of the reasons (as has already been mentioned) is that an ArrayList<Integer> is NOT an ArrayList<String>.
Again, it would seem that setLocationCells() is being far too specific about what it allows, but not knowing what it's used for (or whether you can change it), it's quite difficult to advise on the best course of action.

However, if it was me, and I was faced with this, I'd write a tailored helper method to do the conversion, viz:and I'd be damn annoyed about it too, since the only reason I have to do it is that the writer of setLocationCells() didn't do his (or her) job properly.

HIH

Winston

PS: Welcome to JavaRanch, Jonathan!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!