Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array List

 
Mark Hughes
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Gabriel White
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Gabriel White
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I just checked and java.lang.Number includes java.lang.Long.

Gabe
 
Mark Hughes
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 21362
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
fred rosenberger
lowercase baba
Bartender
Posts: 12180
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
List<Long>

is NOT the same as

List<long>
 
Jacob Rich
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mark Hughes
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mark Hughes
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cool, thanks all for your help
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic