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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:24:59+00:00 2026-06-14T16:24:59+00:00

I have this java method in class called IntArray. The class has methods to

  • 0

I have this java method in class called IntArray. The class has methods to add integers to a set or remove integers from a set,check size of a set, and check if 2 sets are equal. the 2 sets are created using 2 different objects of type IntArray in main, lets say object A and B. equals method supposed to check if two sets of integers are equal. for example set A = {1,2,3} and B = {1,2,3,4}. The method still return true even though one set is a subset of the other set. What exactly I am doing wrong? Thanks.

//part of the code in main
IntArray A = new IntArray();
IntArray B = new IntArray();
if(A.equals(B))
System.out.println("A and B are equal");



 //equals method in IntArray class
 public boolean equals(Object b)
 {
  if (b instanceof IntArray)
    {
      IntArray A = (IntArray) b;
      for (int i = 0; i < data.length; i++)
      if (countOccurrences(data[i]) != A.countOccurrences(data[i]))
      return false;
      return true;
    }
 else return false;  
}
  • 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-14T16:25:00+00:00Added an answer on June 14, 2026 at 4:25 pm
     if (countOccurrences(data[i]) != A.countOccurrences(data[i]))
    

    It may be

     if (countOccurrences(data[i]) != A.countOccurrences(A.data[i]))
    

    EDIT:

    If by equals set you mean each element in the subset are in the same order (A = {1,2,3} and B = {1,2,3}):

    Then, you want to check if two subsets of integers are equal using equals method:

    if (data[i].equals(A.data[i]));
    

    Make sure to only compare the two sets when both have the same length. Otherwise, return false.

    If your definition of equals set means two sets with the same elements, without mattering their position:

    You should check if you countOccurrences is doing something like this:

    public int countOccurrences(int element) 
    {
         int count = 0;
         for(int i = this.data.length - 1; i >= 0; i--) 
            if(this.data[i] == element) 
              count ++;
        return count;
    }
    

    In this latter case you should keep if (countOccurrences(data[i]) != A.countOccurrences(data[i])).

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

Sidebar

Related Questions

i have written a method in java like this public boolean ADD(String ID,String Name,String
I have a public method (called getMusic) in a class called Favorites. getMusic has
I am working in Java. I have an class called Command. This object class
i have a class called LoadDaoHbm.java in which my method is @Override @Transactional(propagation =
I have a Java class called Game which has a non-static integer called score.
Java Code In Java code I have class called IdentificationResult which has 3 members:
Why does Java not have a file copy method? This seems like such an
I have this java servlet that grabs information from a form, I need to
I have a short question i have wrote this in java. Old code: class
Suppose I have a class called Foo . This class will be modified by

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.