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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:51:29+00:00 2026-05-11T06:51:29+00:00

I have MyClass , which stores two integers, and I have a Vector<MyClass> called

  • 0

I have MyClass, which stores two integers, and I have a Vector<MyClass> called myVector.

Now, I do this:

... myVector.add(new MyClass(1,1)); for(MyClass m : myVector)     System.out.println(m); System.out.println(myVector.size());  myVector.remove(new MyClass(1,1)); for(MyClass m : myVector)     System.out.println(m); System.out.println(myVector.size()); ... 

The problem here is that the object isn’t being removed, as I see when I print the Vector and its size. How could I fix that?

Edit: I can see that it isn’t finding the object and I’ve tested with contains() to be sure. What I need is to compare it by value. If I could overload the == operator I could do it, but I have no idea how to fix this.

Edit 2: Okay, equals() will do what I want it to. But I’m not sure what to put in the hashCode method.

Edit 3: I can find it with contains(), but remove doesn’t remove it.

I can use this to remove it though:

int position = myVector.indexOf(new MyClass(1,1)); myVector.remove(position); 

Which is the same as remove(new MyClass(1,1), except the above code works and remove() doesn’t. Any thoughts?

  • 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. 2026-05-11T06:51:29+00:00Added an answer on May 11, 2026 at 6:51 am

    There are few errors in grammar31‘s answer; fix them up here:

    @Override public boolean equals(Object o) {   if ((o == null) || (o.getClass() != myClass.class)) {     return false;   }   myClass other = (myClass) o;   return (other.a == this.a) && (other.b == this.b); }  @Override public int hashCode() {   return a * 31 + b; } 

    This assumes myClass has int members a and b.

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

Sidebar

Related Questions

I have a class like this: public class myClass { public List<myOtherClass> anewlist =
I have the following structure public class MyClass : MyBaseClass<System.Int32> { } In a
If I have the following code: MyClass pClass = new MyClass(); pClass.MyEvent += MyFunction;
Suppose I have a class public class MyClass { private Set<String> set = new
I have gotten everything working for my class in which I am using Tomcat
I have: class MyClass extends MyClass2 implements Serializable { //... } In MyClass2 is
I have a class: class MyClass: def __init__(self, foo): if foo != 1: raise
I have a generic class public MyClass<TContext, T> where TContext : DataContext that effectively
I have got a template class as follows: class MyClass<T> { T field; public
If I have a class org.foobar.MyClass and want to put it in a JAR

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.