Nested generics problem?

Discussion in 'Java' started by jski, Jan 22, 2009.

  1. jski

    jski Guest

    public class Box<T>
    {

    private T t;

    ...
    }

    import java.util.List;
    public class BoxMain
    {

    public static void main(String[] args)
    {
    Box<Float> box = new Box<Float>();
    List<Box<Float>> boxes = new List<Box<Float>>(); <--- ERROR:
    Cannot instantiate the type

    List<Box<Float>>

    box.add(new Float(3.14156));
    boxes.add(box);
    }
    }

    The ERROR above results in Eclipse. Why? ---jski
    jski, Jan 22, 2009
    #1
    1. Advertising

  2. jski

    Guest

    On Jan 22, 5:49 pm, jski <> wrote:
    > public class Box<T>
    > {
    >
    >    private T t;
    >
    >     ...
    >
    > }
    >
    > import java.util.List;
    > public class BoxMain
    > {
    >
    >    public static void main(String[] args)
    >    {
    >       Box<Float> box = new Box<Float>();
    >       List<Box<Float>> boxes = new List<Box<Float>>();  <--- ERROR:
    > Cannot instantiate the type
    >
    > List<Box<Float>>
    >
    >       box.add(new Float(3.14156));
    >       boxes.add(box);
    >    }
    >
    > }
    >
    > The ERROR above results in Eclipse.  Why?  ---jski


    Your error has nothing to do with generics - List is an interface, and
    cannot be directly instantiated. You could write, for example,

    List<Box<Float>> boxes = new ArrayList<Box<Float>> ();
    // 3.14f is a float literal, which will be boxed to a Float object.
    boxes.add (new Box (3.14f));

    -o
    , Jan 22, 2009
    #2
    1. Advertising

  3. jski

    jski Guest

    The pains of a newbie! THANKS, jski

    On Jan 22, 6:09 pm, Patricia Shanahan <> wrote:
    > jski wrote:
    > > public class Box<T>
    > > {

    >
    > >    private T t;

    >
    > >     ...
    > > }

    >
    > > import java.util.List;
    > > public class BoxMain
    > > {

    >
    > >    public static void main(String[] args)
    > >    {
    > >       Box<Float> box = new Box<Float>();
    > >       List<Box<Float>> boxes = new List<Box<Float>>();  <--- ERROR:
    > > Cannot instantiate the type

    >
    > > List<Box<Float>>

    >
    > >       box.add(new Float(3.14156));
    > >       boxes.add(box);
    > >    }
    > > }

    >
    > > The ERROR above results in Eclipse.  Why?  ---jski

    >
    > java.util.List is an interface, not a class. The line should be:
    >
    > List<Box<Float>> boxes = new ArrayList<Box<Float>>();
    >
    > You can substitute any class that implements List for "ArrayList".
    >
    > Patricia
    jski, Jan 22, 2009
    #3
  4. jski

    Roedy Green Guest

    On Thu, 22 Jan 2009 14:49:27 -0800 (PST), jski
    <> wrote, quoted or indirectly quoted someone
    who said :

    >List<Box<Float>> boxes = new List<Box<Float>>(); <--- ERROR:


    You can have an interface on the left of = but not after new. You must
    instantiate some concrete class that implements List, not List itself.
    i.e. ArrayList, AttributeList, CopyOnWriteArrayList, LinkedList,
    RoleList, RoleUnresolvedList, Stack, Vector



    --
    Roedy Green Canadian Mind Products
    http://mindprod.com

    We are almost certainly going to miss our [global warming] deadline.
    We cannot get the 10 lost years back, and by the time a new global agreement to
    replace the Kyoto accord is negotiated and put into effect, there will probably
    not be enough time left to stop the warming short of the point where we must not
    go. ~ Gwynne Dyer
    Roedy Green, Jan 23, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Thomas G. Marshall

    Nested generics (limited?)

    Thomas G. Marshall, Aug 3, 2004, in forum: Java
    Replies:
    0
    Views:
    473
    Thomas G. Marshall
    Aug 3, 2004
  2. Juergen Berchtel
    Replies:
    1
    Views:
    5,975
    John C. Bollinger
    May 20, 2005
  3. Replies:
    3
    Views:
    460
    Victor Bazarov
    Dec 7, 2007
  4. Jeff

    nested classes in .NET generics

    Jeff, Nov 27, 2008, in forum: ASP .Net
    Replies:
    1
    Views:
    497
    bruce barker
    Nov 27, 2008
  5. Soul
    Replies:
    0
    Views:
    508
Loading...

Share This Page