Forums Register Login

Array List

+Pie Number of slices to send: Send
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.


Can ye show me the error of my ways please

Mark
+Pie Number of slices to send: Send
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.

This code works:

List<Long> lngDate = new ArrayList<Long>();

HTH

Gabe
+Pie Number of slices to send: Send
Yes, I just checked and java.lang.Number includes java.lang.Long.

Gabe
+Pie Number of slices to send: Send
you said that

This code works:

List<Long> lngDate = new ArrayList<Long>();


looks the same as mine, or is it different.

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
+Pie Number of slices to send: Send
 


List<Long> lngDate = new ArrayList<Long>();

looks the same as mine, or is it different.



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.

Henry
+Pie Number of slices to send: Send
List<Long>

is NOT the same as

List<long>
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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.

Am i right there.

Mark
+Pie Number of slices to send: Send
Case is important here.

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.
+Pie Number of slices to send: Send
cool, thanks all for your help
+Pie Number of slices to send: Send
Just to clarify what's already been said...

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.
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
https://gardener-gift.com


reply
reply
This thread has been viewed 18268 times.
Similar Threads
String Tokens
Is there a way to convert a ArrayList of Longs to a long array (long[])
Convert ListArray to string array
casting into primitive wrapper class
Using lists in a jsp
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 17:42:21.