Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7965495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T06:10:28+00:00 2026-06-04T06:10:28+00:00

I intend to create a function with the following signature – public static List<?

  • 0

I intend to create a function with the following signature –

public static List<? extends Model> getList(Model T, int numberOfItems, StringReader reader)

Now, inside this function, I want to create an ArrayList that will contain the same type of objects as T or any of its subclasses, i.e T could be a Model object or any of its subtypes.

So far, I tried –

   List < ? extends T> list = new ArrayList();

But it didn’t work. How to get this to work?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T06:10:30+00:00Added an answer on June 4, 2026 at 6:10 am

    You’ll have to pass in the Class instance as a type marker. Something like this:

    public static <T extends Model> List<T> getList(
        Class<T> c, int numberOfItems, StringReader reader)
    {
      final List<T> l = new ArrayList<T>();
      try { for (int i = 0; i < numberOfItems; i++) l.add(c.newInstance());
      } catch (Exception e) { throw new RuntimeException(e); }
      return l;
    }
    

    I don’t know how you want to use the StringReader so I left it aside. Call the method with a class literal. Say you’ve got a SubModel extends Model, then

    final List<SubModel> l = getList(SubModel.class, 10, reader);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

following piece of code: class a{ public function __get($key) { if($key == 'obj') {
I intend to create a checker function which will be used by another functions.
I intend to create asp.net pages using Visual Studio 2008. Preferably, the pages should
I have a data that looks like this . And I intend to create
Please help with this error .... In the following code the get info function
I'm trying to create a basic function to swap the endianness of a short
I've been experimenting with ASP.NET MVC and following this tutorial to create the basic
I'm writing a class which I intend to use to create subroutines, constructor as
My intend is to create a simple easily debuggable console application which will be
I am trying to use set function in App Engine, to prepare a list

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.