Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > is it ok to do this... and generics

Reply
Thread Tools

is it ok to do this... and generics

 
 
sparkles
Guest
Posts: n/a
 
      03-29-2005
sorry about the subject - i'm not quite sure what it is i'm asking is called

is there anything wrong with doing this...?

ArrayList al = new ArrayList();
al.add(new MyClass(1, 2, 3, 4)); <-----

or is it better to do this?

ArrayList al = new ArrayList();
MyClass class = MyClass(1, 2, 3, 4);
al.add(class);

also if the arraylist is instantiated using generics as such

ArrayList<MyClass> al = new ArrayList<MyClass>();

does that mean that al.get() will return a MyClass or do i still have to
cast it?

TIA
 
Reply With Quote
 
 
 
 
Lee Weiner
Guest
Posts: n/a
 
      03-29-2005
In article <4249d175$(E-Mail Removed)>, sparkles <(E-Mail Removed)> wrote:
>sorry about the subject - i'm not quite sure what it is i'm asking is called
>
>is there anything wrong with doing this...?
>
>ArrayList al = new ArrayList();
>al.add(new MyClass(1, 2, 3, 4)); <-----
>
>or is it better to do this?
>
>ArrayList al = new ArrayList();
>MyClass class = MyClass(1, 2, 3, 4);
>al.add(class);
>


It's called an anonymous object (there is no named variable referencing the
object), and, no, there's nothing wrong with it if you have no other use for
the "class" variable, IMO.

>also if the arraylist is instantiated using generics as such
>
>ArrayList<MyClass> al = new ArrayList<MyClass>();
>
>does that mean that al.get() will return a MyClass or do i still have to
>cast it?


al.get() will return a MyClass object. There's no need to cast.

Lee Weiner
lee AT leeweiner DOT org
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
generics depending on generics Soul VHDL 0 02-02-2009 09:14 AM
int[] and Integer[], and generics kelvSYC Java 2 04-04-2007 09:43 AM
Generics and associative array of keys and diverse objects Frank Fredstone Java 1 09-19-2006 11:42 AM
tutorial on generics and threads and io puzzlecracker Java 7 01-31-2006 03:38 PM
Can't convert a generics list of objects into a generics list ofinterfaces Juergen Berchtel Java 1 05-20-2005 02:07 PM



Advertisments