Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Java ArrayList

Name: Anonymous 2009-08-29 0:52

Oh hai
I'm kinda new to programming (I've used Scheme, a little BASIC, and Python) and am trying to learn Java.
When I try to implement an ArrayList class, eclipse indicates that I should have the syntax "Arraylist<E>()". I know the () brackets can be empty, but what is the purpose of the <> brackets? I thought it was a type specification, but when I put in my type 'Record' into there, and attempt the following:
ArrayList records = new ArrayList<Record>();
//stuff
//new method
return records.get(0).text(); //text is a valid method of Record
it seems to think that the return type of .get(0) is a generic Object.

Wut happin?

Name: Anonymous 2009-08-29 2:19

>>5
They parameterize the type. The definition of ArrayList includes a type variable, so that you can specify in your code what type of value you'll be storing in it (and what return value some of its methods should have). You pass a type (such as String) between the angle brackets. If you don't, it defaults to Object.

Java is not the greatest language for learning any concept, least of all parameterized types. You might look up a Haskell tutorial and just concern yourself with the type system for the time being.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List