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

Pages: 1-

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 0:55

Name: Anonymous 2009-08-29 0:57

>>2
Believe it or not , I tried that.

Name: Anonymous 2009-08-29 1:10

>>1
Why are you implementing a class that's already in the standard library?

Name: Anonymous 2009-08-29 1:14

>>4
Whoops, sorry, meant "use" the ArrayList, lol.
But yeah, I just want to know what those <> brackets do.

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.

Name: Anonymous 2009-08-29 2:20

Generics.

Unfortunately, it's a hack and still results in type erasure, so they are not as neat as they first appear. Still, they make it many more times nicer than in 1.4.

Name: Anonymous 2009-08-29 2:46

>>6
So it's pretty much what I thought it was :\
Guess I'll have to keep fiddling around, thanks guys.

Name: Anonymous 2011-01-31 19:57

<-- check em dubz

Name: Anonymous 2011-02-03 1:43

Name: Anonymous 2011-02-03 2:11

Name: Anonymous 2011-02-04 15:00

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