dlite922 1,584
Recognized Expert Top Contributor
I have a list of objects, the object has a String and an Int in it. I need to check to see if an item is in the list, if so increment the integer and save it back to the list.
I believe I've done everything OK up to the point of checking to see if an object is in the list by overloading the equals() and hashCode() functions.
I don't have any errors and compiles except my list contains two entries of the same name (String) instead of having an entry and the counter (Int) being 2. When printed: testingTitle 1
testingTitle 1 It should be: testingTitle 2
Here's the relevant code: -
-
while(titleItr.hasNext()) {
-
rank = 1;
-
String title = (String) titleItr.next();
-
TitleEntity te = new TitleEntity(title, rank);
-
-
if(resultList.contains(te)) {
-
int idx = resultList.indexOf(te);
-
te = resultList.get(idx);
-
te.setRank((te.getRank()+1));
-
resultList.set(idx, te);
-
} else {
-
resultList.add(te);
-
}
-
}
-
-
te is the object with the string (called title) and integer (called rank) members
resultList is the array list of them.
I"m assuming contains(te) gives me the correct entity since I've overloaded equals(). correct?
EDIT: just found out my program never goes into that if, but always the else. what do I need for contains() to work? Here's the overloaded equals(), compareTo() and hashcode() in TitleEntity() class. -
-
public int compareTo(TitleEntity te) {
-
return this.rank - te.rank;
-
}
-
-
public boolean equals(TitleEntity te) {
-
return (this.title.equals(te.title));
-
}
-
-
public int hashCode() {
-
return (title.hashCode());
-
}
-
-
Any help would be appreciated as always!
Dan
3 8781 dlite922 1,584
Recognized Expert Top Contributor
SOLVED:
equals cannot be overloaded (same name different param) but can be overided(same name and same parameters). So I changed equals() to this: -
public boolean equals(Object o) {
-
if(!(o instanceof TitleEntity)) {
-
return false;
-
}
-
TitleEntity te = (TitleEntity)o;
-
return this.title.equals(te.title);
-
}
-
Now contains finds it and a few other minor changes, I get the desired result.
Cheers,
Dan
Actually equals can be overloaded as you found out in the code. You may just get unexpected results (like you got), that's all.
Also read overriding equals and hashCode and compare it with what you have done.
dlite922 1,584
Recognized Expert Top Contributor @r035198x
At first glance, it looks the same, except the check to see if the object is in-fact itself first. This would never happen in my code, but I added it anyway :)
Thanks!
Dan
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: William Hanlon |
last post by:
Hi,
I would like to use the Python C API to use python to access functions
originally written in C. The objects that I would like to pass to python
have multi-dimensional arrays.
How do I include arrays as object member? And how is it declared in the
PyMemberDef array?
If you can not use C arrays, I thought perhaps then I should use Python
|
by: Steven T. Hatton |
last post by:
As far as I know, there is no way to provide a default value for the
argument to the constructor A::A(char (&array)) in this example.
Correct?
struct A{
A(char (&array) ){
std::copy(&array,&array,&_array);
}
char _array;
};
|
by: Kaz Kylheku |
last post by:
Given some class C with array T x, is it possible to get a
pointer-to-data-member to one of the elements?
&C::x gives us a pointer-to-member-array: T (C::*).
But I just want to get a T C::* pointing to a selected array element,
so that I can later use an instance c of that class to pick out that
array element: c->*ptr.
This syntax, for instance, doesn't work: &C::x. My compiler thinks
|
by: jut_bit_zx |
last post by:
class A
{
public:
A();
virtual ~A(){}
....
private:
int m_iarray;
}
|
by: Tamir Khason |
last post by:
How can I set the type of the object added to ArrayList (type of Array List
Members)
Here is the code:
protected ArrayList tabs = new ArrayList();
public ArrayList Tabs
{
| |
by: Zorpiedoman |
last post by:
The problem is this:
I have a list box. I set an array list as the datasource. I remove an item
from the array list. I set the listbox datasource to nothing. I set the
listbox datasource to the array list again to refresh. Click on an item in
the list, and an Indexing error comes up. (in non-user code, it is trying to
get an element from the array that is greater than the number of items in the
array.)
To reproduce:
|
by: gg9h0st |
last post by:
i'm a newbie studying php.
i was into array part on tutorial and it says
i'll get an array having keys that from member variable's name
by converting an object to array.
i guessed "i can get public members but not protected, private, static
members"
|
by: sks |
last post by:
hi ,
i Have a code snippet as follows
class ABC
{
int &r;
ABC(int a=0): r(a) {}
};
int main()
{
|
by: Peng Yu |
last post by:
Hi,
I'm wondering if there is a way to initialized a member array just as
the initialization of a member variable? Or I have to initialized the
array inside the function body of the constructor?
Thanks,
Peng
#include <iostream>
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |