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 9162187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:09:04+00:00 2026-06-17T14:09:04+00:00

I have a generic class that should creates a collection of comparables stored in

  • 0

I have a generic class that should creates a collection of comparables stored in an array. I am very unclear on the way that comparables and generics work.

public class OrderedCollection<T extends Comparable<? super T>> 
{
private T collection[]; // the collection
private int size, tempValue; // how many elements currently stored

/**
 * Constructor allocates array and initializes size
 * @param size the number of elements stored
 */
    public OrderedCollection (int capacity) 
    {
    collection = (T[]) new Comparable[capacity];
    size = 0;       
    }
}

First of all, what kind of collection is collection (array, list etc). It is never explicitly instantiated as new Array[] so I am curious how this is supposed to create an array.

Second, there needs to be a method that inserts a designated value (for testing purposes I have been using’5′) and assigning it to collection[0]. However, when I return collection[0], it returns as null. Here is the insert method:

public void insert(T x) 
{
    collection[0] = x;
}

Nothing fancy. I would greatly appreciate some clarification as to why the collection returns null and how I should go about adding the designated x value to the collection.

  • 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-17T14:09:05+00:00Added an answer on June 17, 2026 at 2:09 pm

    The normal convention is:

    private T[] collection;
    

    Yours was a C compatibility syntax.

    public void add(T x) {
        if (size >= collection.length)
            throw new IllegalStateException();
        collection[size] = x;
        ++size;
    }
    

    Collection is an interface for any kind of collection. Implementing classes are HashTree, ArrayList etcetera.

    This is a difference to some other languages, that java has not a few “collection” classes like JavaScript, but a few interfaces, that have an implementation you can choose. So you have for a Map the choice of HashMap, TreeMap, LinkedHashMap and so on. So in the API you put the interface, but the implementation uses the technical advantages of some implementation.

    For instance traversing a TreeMap is ordered by the keys. Traversal of a LinkedHashMap is ordered by the order of insertion.

    That about null must be a programming error.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a utility class that needs to work on a generic Class but
I have a generic class that needs to limit an enum depending on the
I have a generic class that takes a type T . Within this class
I have a Generic Base Class that I want to allow one of two
Suppose I have a Generic abstract class that provides some default constructor functionality so
I have a class that inherits from a generic dictionary as follows: Class myClass
I have generic type that looks like: public class GenericClass<T, U> where T :
I have a Base class that needs a Generic type. That can be either
I have a repository class that inherits from a generic implementation: public namespace RepositoryImplementation
I have a mutable class that I'm using as a key to a generic

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.