Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

ArrayList<String> to int.  RSS feed

 
Harold Lauder
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hello. I have some problems with this code. This is seafight from Java HeadFirst. How I notice ArrayList is massive which contain list of Strings, not ints and this causing a problem:

SimpleDotComGame.java:46: error: incompatible types: int[] cannot be converted
o ArrayList<String>
        theDotCom.setLocationCells(locations);

I have tried to put this code but I created only even more errors, because I dont understand how call this method in my original code:



Can someone explain to me how can I unite ArrayList<String> with int []? Sorry for vague explanation.
 
Stephan van Hulst
Saloon Keeper
Posts: 7817
142
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To convert an int[] to a List<String>, I would first use Arrays.stream(), and then IntStream.mapToObj() and Stream.collect().

However, I'm not convinced that you need either type. If you are interested in integers, you can make DotCom hold a List<Integer>, and pass a Collection<Integer> when you construct an instance.
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Harold,a warm welcome to javaRanch.

your code is quite confusing.taking too much time to understand,try to write indently along with comments explaining what you are doing or wants to do.

in your case you should use ArrayList<Integer> instead of ArrayList<String> and you have also gave wrong argument type to method setLocationCells give the argument as Arrays.asList(locations) to method or simply use ArrayList<Integer> instead of array.

Thankyou,

kind Regards,
praveen.
 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also never write == true or == false. Both are poor style and error‑prone; you might write = instead of == by mistake.
Never
while (b == true) ...
always
while (b) ...
Never
while (b == false) ...
always
while (!b) ...

And welcome again
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!