Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Exclamation Generic Array Creation - NOT ALLOWED?(java 5.0)

    private <K, V> V[] getMapValuesAsArray(Map<K, V> map) {
    V[] valueArray= new V[map.entrySet().size()];
    int count=0;
    for(Map.Entry<K, V> entry : map.entrySet()) {
    valueArray[count] = entry.getValue();
    count++;
    }
    return valueArray;
    }

    In the above code,

    Why cannot the below line be allowed in java?
    V[] valueArray= new V[map.entrySet().size()];

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Because at compile time Java does not know what "V" is and it is, through erasure, reduced to Object. See the Generics Tutorial.

  3. #3
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Default Strict Typing

    @masijade
    I really can not figureout , why does it need to know at compile time.

    I think, if java can support, strict typing, this could be posible. (I guess).

    So why not java can introduce some kind of annotation, to support strict typing at class/method level and only if that annotation is present, this kind of code snippet can be allowed.

Similar Threads

  1. Java Generic Container Question - please help!!!
    By zhoujackji in forum New To Java
    Replies: 3
    Last Post: 11-13-2010, 12:12 PM
  2. Generic Collections---Dysfuntional Java
    By beezerbutt in forum New To Java
    Replies: 6
    Last Post: 06-28-2009, 08:37 PM
  3. Int Array Allowed in main() Method?
    By Starclopsofish in forum New To Java
    Replies: 19
    Last Post: 10-03-2008, 05:22 PM
  4. Java GUI creation help?
    By sabrown311313 in forum New To Java
    Replies: 4
    Last Post: 09-13-2008, 07:28 AM
  5. Generic array
    By eva in forum New To Java
    Replies: 3
    Last Post: 12-23-2007, 01:12 AM

Posting Permissions

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