• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

ArrayList of double and float

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is not different that creating a list of Strings except for the type parameter.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 76394
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Saloon Keeper
Posts: 14260
321
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should totally get points for that though :P
 
Campbell Ritchie
Marshal
Posts: 76394
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Roel De Nijs wrote:. . . "I don't know them because Campbell said not to use them" . . .

 
Let nothing stop you! Not even this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic