Hey guys, i want to make an array List to store longs, but when i do it i get an error syntax error on token "long", dimensions expected after this token. this is how i wrote it.
When you use generics you will have to include a reference to a type of object that is going in the List. In this case you are trying to tell the ArrayList to reference only values of type long, or int or double, etc...
You need to declare the objects that will be referenced in a generic by usinig big D Double and Big I Integer and Big L Long as these are object types.
I'm not too sure if you can box a Long as an object type but check your API and you will see.
yet i get an error with mine. im trying to add a long type to it too. could ya show a little more code of maybe it being implemented if you have time. thanks
This code is *not* the same as yours... but let's back up a bit.
The collection classes are designed to store objects -- not primative values. So declaring a ArrayList<long> is not valid. What this declaration does is declare an array list of Long -- the java.lang.Long class to be exact. This is an object type and is valid.
And the reason this will work is because of another feature of Java -- autoboxing. When you add() a primative long, the compiler will figure out that there is no such add() method, that takes a long, and automatically create a java.lang.Long object for you.
I thought arraylist were restricted to being coded as arraylist<object> since they fall under the Object superclass in the inheritance tree...But meh, i dont know i might have misunderstood.
Originally posted by jacob rich: I thought arraylist were restricted to being coded as arraylist<object> since they fall under the Object superclass in the inheritance tree...
Welcome to JavaRanch!
Java has a "single-rooted hierarchy," meaning that every class is a subclass of Object.
What you're probably recalling is that references in Java collections (like a List) are automatically upcast to type Object. Prior to the introduction of generics with Java 1.5, there was no type checking when adding references to a collection -- it was left to the programmer to ensure that only the proper types were added. And these references needed be to explicitly downcast when removed from a collection. With generics, a specific type can be associated with the collection, allowing the compiler to provide type checking and implicit downcasting.
But note that Java is case sensitive, so arraylist is not the same as ArrayList, object is not the same as Object, and so on.
Ok so ArrayList can only store objects, so by declaring ArrayList<long> is not valid as long in lower case is a primitive value. But by declaring it as ArrayList<Long> with Long as upper case it tells the ArrayList to expect Objects of type long so then you could declare a primitive long variable and add that primitive long variable to the ArrayList casted to Object Long.
it tells the ArrayList to expect Objects of type Long so
Your ArrayList will only accept Long objects, however, as explained in Henry's earlier post you can add a primitive long, because autoboxing will do the conversion for you in the background.
If you add a long (primitive) to a List<Long>, there are two things happening: First, the primitive value will be boxed as a wrapper type Long, and then the reference to that new Long object will be upcast to type Object for storage in the List.
Post by:autobot
We're being followed by intergalactic spies! Quick! Take this tiny ad!
a bit of art, as a gift, that will fit in a stocking