• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ArrayList of double and float

 
shweta patiljadhav
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please give me an example of ArrayList of double and floats....I am able to create one with String and StringBuilder only....
TIA
 
Paweł Baczyński
Bartender
Posts: 1876
35
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is impossible to have an ArrayList of doubles or floats (or any other primitive type).
You can create an ArrayList of Doubles or Floats.
 
shweta patiljadhav
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

thank you very much! please give me an example code of making ArrayList of Double.

TIA
 
Paweł Baczyński
Bartender
Posts: 1876
35
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is not different that creating a list of Strings except for the type parameter.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shweta patiljadhav wrote:Please give me an example of ArrayList of double and floats....I am able to create one with String and StringBuilder only....

As Paweł already mentioned you can't have an ArrayList with a primitive type. You can only have ArrayList with objects. Luckily for us, the Java API has for every primitive data type a primitive wrapper class (e.g. int -> Integer, float -> Float, and so on). So you must use the primtive wrapper class to create an ArrayList and you won't create an ArrayList of doubles/floats but you create an ArrayList of Doubles/Floats. Thanks to the autoboxing feature (which was added in Java 1.5), a primitive is automagically converted to an instance of the corresponding primitive wrapper class.

So let's turn all this boring text into some exciting code snippets Trying to create an ArrayList of primitive data types will not compileYou must use the corresponding primitive wrapper class, this code snippet compiles successfullyAnd thanks to autoboxing, primitive data types are automagically converted to an instance of the corresponding primitive wrapper class. So this code snippet will also compile successfully

But this code snippet has a compiler error. Do you know which line is causing this error and why?

Hope it helps!
Kind regards,
Roel
 
Campbell Ritchie
Sheriff
Posts: 51349
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would you want to use floats in the first place? Don't. Unless some part of another API requires floats. Stick to doubles.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Why would you want to use floats in the first place?

Because floats are a topic on the OCA exams and "I don't know them because Campbell said not to use them" is probably not one of the possible answers
 
Stephan van Hulst
Bartender
Pie
Posts: 6503
83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should totally get points for that though :P
 
Campbell Ritchie
Sheriff
Posts: 51349
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:. . . "I don't know them because Campbell said not to use them" . . .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic