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 4 of 4

Thread: Parameterized array type as a method parameter

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Parameterized array type as a method parameter

    I cannot find an article on how to do this, so hopefully someone can help me here. I have the following method:
    public static void doSomething(List<? extends GenericClass> input)
    {
    	// op
    }
    ^ This compiles and works, ensuring I can only pass in a List of a type extending GenericClass.

    But now I want it to accept an Array instead of List. This is where I'm stuck:
    public static void doSomething(<? extends GenericClass>[] input)
    {
    	// op
    }
    ^ A wrong guess at the syntax which does not compile. Anyone know the solution? Yes, one is to convert Array into ArrayList before calling the 1st method above, but I'd rather bypass this if possible.
    Last edited by Lediator; August 1st, 2014 at 01:12 AM. Reason: used highlight for code


  2. #2
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Parameterized array type as a method parameter

    Arrays are not generic.
    What you want is to do:
    public static void doSomething(GenericClass[] input)

  3. The Following User Says Thank You to Cornix For This Useful Post:

    Lediator (August 1st, 2014)

  4. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Parameterized array type as a method parameter

    Welcome to the Forum! Thanks for taking the time to learn to post code correctly, and if you haven't already, please read this topic to see other useful info for newcomers.

    You may find the discussion on this page helpful.

  5. #4
    Junior Member
    Join Date
    Jul 2014
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Parameterized array type as a method parameter

    Quote Originally Posted by Cornix View Post
    Arrays are not generic.
    What you want is to do:
    public static void doSomething(GenericClass[] input)
    Yes exactly what I was looking for! Amazing how I didn't know that about arrays. Thanks for that.

Similar Threads

  1. Replies: 2
    Last Post: August 4th, 2013, 08:57 PM
  2. Replies: 2
    Last Post: January 14th, 2013, 03:22 PM
  3. can I create an array in a parameterized class
    By mia_tech in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 7th, 2012, 07:13 PM
  4. Replies: 3
    Last Post: May 27th, 2012, 11:11 AM
  5. array as parameter
    By pjo in forum Java Theory & Questions
    Replies: 3
    Last Post: March 16th, 2012, 07:27 PM