Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: How to convert an array of integer to generic type?

  1. #1
    Member
    Join Date
    Feb 2011
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to convert an array of integer to generic type?

    I have an array,
    private T[] elements;
    . I would like to do this ideally:

    if(elements[index] == null){
        elements[index] =  0;
    }
    elements[index] = elements[index] + 1;

    But I get the error of "cannot cast from int to T." How can I check my array for a null space and change it to zero if null is encountered (and then increment the 0 to 1)?
    Last edited by Deep_4; November 7th, 2012 at 12:42 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Convering int to generic type?

    What type is T? It may not even make sense to cast an int to T, for example if elements is meant to hold String objects.

  3. #3
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Convering int to generic type?

    if(elements[index] == null){
    elements[index] = 0;
    }
    elements[index] = elements[index] + 1;


    But I get the error of "cannot cast from int to T." How can I check my array for a null space and change it to zero if null is encountered (and then increment the 0 to 1)?
    Why set it to zero in the first place then? why not just say if i of elements == null then i of e = 1;

Similar Threads

  1. having problem declaring a generic type array in OrderSet class
    By mia_tech in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 10th, 2012, 06:39 PM
  2. How to convert from type double to type int?
    By rph in forum Object Oriented Programming
    Replies: 7
    Last Post: July 25th, 2011, 04:21 AM
  3. Creating array from generic type objects?
    By AndrewMiller in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 13th, 2010, 09:22 PM
  4. Not Generic; cannot be parameterized
    By javaoo in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 15th, 2010, 09:53 AM
  5. generic class
    By fireatmuself in forum Collections and Generics
    Replies: 4
    Last Post: November 17th, 2009, 06:06 AM