This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Array List Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Array List" Watch "Array List" New topic
Author

Array List

Mark Hughes
Ranch Hand

Joined: Jul 14, 2006
Posts: 146
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

Joined: Mar 02, 2003
Posts: 233
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

Joined: Mar 02, 2003
Posts: 233
Yes, I just checked and java.lang.Number includes java.lang.Long.

Gabe
Mark Hughes
Ranch Hand

Joined: Jul 14, 2006
Posts: 146
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
Sheriff

Joined: Sep 28, 2004
Posts: 18874
    
  40


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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11351
    
  16

List<Long>

is NOT the same as

List<long>


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jacob Rich
Greenhorn

Joined: Jan 02, 2007
Posts: 27
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.


~"Great is the art of beginning, but greater is the art of ending."~
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Mark Hughes
Ranch Hand

Joined: Jul 14, 2006
Posts: 146
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

Joined: Aug 05, 2005
Posts: 3602
    
  15
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.


Joanne
Mark Hughes
Ranch Hand

Joined: Jul 14, 2006
Posts: 146
cool, thanks all for your help
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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.
 
GeeCON Prague 2014
 
subject: Array List