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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:53:41+00:00 2026-05-30T22:53:41+00:00

This is a homework problem that I am having difficulty with. The skeleton for

  • 0

This is a homework problem that I am having difficulty with. The skeleton for the class is given below:

public class Storage<E> implements java.util.Collection<E> {
private Object[] data = new Object[256];
private int nextEmptySlot = 0;

@Override
public java.util.Iterator<E> iterator() {
    // returns a class that iterates over the data array
    return new java.util.Iterator() {
        // insert the body of the inner class here
        // 3 methods: remove(), hasNext(), next()
        };
    }
//Override all methods required by the interface
}

Now the trouble I’m having is writing functions such as contains(Object o), because the template does not force E to be comparable. How can I treat the templated type E as comparable so that I can use equals and compateTo? I know there are ways to do it in the class declaration but the class declaration is given and does not require E to be comparable…

  • 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-05-30T22:53:42+00:00Added an answer on May 30, 2026 at 10:53 pm

    You should not need compareTo() because this would give you a definition of the ordering of the elements (though it can tell you if two objects are equal). Unless order is important here, this won’t be what you want anyway.

    If the assumption that order is not required is correct, then all you should need to rely on is the equals() method of each instance within the collection.

    1. If E overrides the equals() and hashCode() methods properly, E itself will be able to determine if the element is within this collection.
    2. If it has not overrode the equals() method, then the instance will rely on Object‘s implementation of equals() which simply compares the references (if they’re both non-null).

    I would suggest making sure you are clear on the requirement of this assignment first. If the requirements state something more specific, you may be able to determine what equality is within your collection. If you cannot get anything more clear, I believe your only choice is to rely on E‘s implementation of equals().

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

Sidebar

Related Questions

This is homework. Beginning Java class. Still wrapping my head around this stuff. The
This is a question for a homework problem that I cant figure out: Question
This is a homework problem that I have. I have been doing some research
Disclaimer: this is a (frustrating) homework related problem. I'm having odd results when I
I was taught HMM and given this homework problem. I understood a part of
I have a homework problem that asks: Desgin a derived class GraduateStudent from base
I'm trying to solve this problem, its not a homework question, its just code
Basically i am having problems with my code - this is homework so would
I'm working on this homework that's kind of confusing me... I am provided with
Please note that this is not homework and i did search before starting this

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.