Results 1 to 7 of 7
  1. #1
    pyler is offline Member
    Join Date
    Jul 2012
    Posts
    18
    Rep Power
    0

    Default Creating two typed generic array

    I'm trying to create a generic array with two types as in
    Java Code:
    <E,T>[] somearray=(<E,T>[]) new Object[10];
    but eclipse keeps giving me a reference type expected error.

    Is there a way to create an array of two types in java?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,734
    Blog Entries
    7
    Rep Power
    21

    Default Re: Creating two typed generic array

    You can't define generic arrays at all (how would you instantiate such an array? The language doesn't have a construct for it).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,923
    Rep Power
    5

    Default Re: Creating two typed generic array

    Use a List instead.

    Java Code:
    class TwoTypes<T,S> {
       private T type1;
       private S type2;
       public TwoTypes( T type1, S type2) {
           this.type1 = type1;
           this.type2 = type2;
       }
       public T getType1() {
            return type1;
       }
       public S getType2() {
            return type2;
       }
    }
    
    // Then
    
    List<TwoTypes<String,Integer>> myTypes = new ArrayList<>();
    // You can also do this, but it isn't as clean.
    @SuppressWarnings("unchecked")
    TwoTypes<String,Integer> [] m = new TwoTypes[10];
    //
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,277
    Rep Power
    6

    Default Re: Creating two typed generic array

    That code makes me sick to my stomach!

    (ego-less programming)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,923
    Rep Power
    5

    Default Re: Creating two typed generic array

    What is it you don't like about it? Generics? Non specific get methods? The fact that it is pretty useless? :)

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,277
    Rep Power
    6

    Default Re: Creating two typed generic array

    No that array surpresswarnings hack that you yourself already define as "not so clean" :p
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,923
    Rep Power
    5

    Default Re: Creating two typed generic array

    Well, you can leave it off if you want. But in this case it is a non-issue because that statement is perfectly legal and will not cause problems. So it is best to eliminate the unchecked warnings. You could also do it like the following. But still need the warning suppression but for a different reason.
    Java Code:
    @SuppressWarnings("unchecked")  
    TwoTypes<String,Integer> [] m = (TwoTypes<String,Integer>[])new TwoTypes[10];
    There are times when the compiler can't guarantee things the programmer can. Usually involves working with arrays, generics and casting. And capture bridges also come to mind. I try to avoid all of them if I can.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 4
    Last Post: 04-02-2012, 02:50 PM
  2. Creating generic wrapper of return type
    By jackett_dad in forum Advanced Java
    Replies: 1
    Last Post: 03-16-2012, 06:53 PM
  3. Creating an array of generic type
    By colerelm in forum New To Java
    Replies: 1
    Last Post: 10-19-2011, 04:58 AM
  4. Creating generic arrays
    By stijn1989 in forum Advanced Java
    Replies: 6
    Last Post: 11-18-2009, 02:26 PM
  5. Generic array
    By eva in forum New To Java
    Replies: 3
    Last Post: 12-23-2007, 01:12 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •