Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with Populating an ArrayList of Floats

 
Rob Sweeny
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

How do I populate an ArrayList of Floats?
The below code gives me errors on compilation.





c:\code>javac LegalKeeperFish.java
LegalKeeperFish.java:7: cannot find symbol
symbol : method add(double)
location: class java.util.ArrayList<java.lang.Float>
___________ fishLengthList.add(10.0);
______________________^

Thanks

p.s. Is this the correct forum for a question like this?
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try declaring all your numbers like this:

fishLengthList.add(10.0f);


Putting an f after the number makes it a float, as opposed to a double.
 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob:

You've got your ArrayList set up correctly. The problem with your code is that floating point numbers default to doubles unless you mark them as floats. So, if you want Java to know you mean floats rather than doubles, you'll need to add an 'f' to the number, like this: 23.05f.

John.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic