-
February 21st, 2009, 12:35 PM
#1
List<float> = missing token?
Hi I just started up java recently, and I've been struggling on how to make a list of floating point numbers. The error I get is "dimensions expected after this token" but all examples on the web don't really point to a reason as to what or how i should approach this. THanks.
-
February 21st, 2009, 01:02 PM
#2
Re: List<float> = missing token?
Originally Posted by taiL
Hi I just started up java recently, and I've been struggling on how to make a list of floating point numbers. The error I get is "dimensions expected after this token" but all examples on the web don't really point to a reason as to what or how i should approach this. THanks.
It's not quite clear what your problem is. But here are a few pointers.
First, you cannot store primitives in Java collections. What you need to do is to store the corresponding wrapper class. The corresponding wrapper to a float is a Float.
Second, List is an interface and not a concrete class. You can declare a variable of type List but you need to create and assign a concrete class object to it, like
List<Float> l = new ArrayList<Float>();
or
List<Float> l = new LinkedList<Float>();
ArrayList and LinkedList both implement the List interface (but have different properties).
Now you can start adding Float objects to the List<Float> you've created. In fact you can even add float primitives thanks to a Java feature called autoboxing. Autoboxing makes sure a primitive (like a float) gets converted to its corresponding wrapper class (like a Float) and back agan when it needs to be.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|