473,657 Members | 2,478 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Increment object member in an Array List

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:
Expand|Select|Wrap|Line Numbers
  1.  
  2.             while(titleItr.hasNext()) {
  3.                 rank = 1;
  4.                 String title = (String) titleItr.next();
  5.                 TitleEntity te = new TitleEntity(title, rank);
  6.  
  7.                 if(resultList.contains(te)) {
  8.                     int idx = resultList.indexOf(te);
  9.                     te = resultList.get(idx);
  10.                     te.setRank((te.getRank()+1));
  11.                     resultList.set(idx, te);
  12.                 } else {
  13.                     resultList.add(te);
  14.                 }                
  15.             }
  16.  
  17.  
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.

Expand|Select|Wrap|Line Numbers
  1.  
  2.     public int compareTo(TitleEntity te) {
  3.         return this.rank - te.rank;
  4.     }
  5.  
  6.     public boolean equals(TitleEntity te) {
  7.         return (this.title.equals(te.title));
  8.     }
  9.  
  10.     public int hashCode() {
  11.         return (title.hashCode());
  12.     }
  13.  
  14.  
Any help would be appreciated as always!




Dan
May 20 '09 #1
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:

Expand|Select|Wrap|Line Numbers
  1.     public boolean equals(Object o) {
  2.         if(!(o instanceof TitleEntity)) {
  3.             return false;
  4.         }
  5.         TitleEntity te = (TitleEntity)o;
  6.         return this.title.equals(te.title);
  7.     }
  8.  
Now contains finds it and a few other minor changes, I get the desired result.

Cheers,



Dan
May 20 '09 #2
r035198x
13,262 MVP
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.
May 21 '09 #3
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
May 21 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
2123
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
16
2562
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; };
3
5843
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
3
5932
by: jut_bit_zx | last post by:
class A { public: A(); virtual ~A(){} .... private: int m_iarray; }
4
7082
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 {
11
8548
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:
4
3187
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"
4
1558
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() {
2
2538
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>
0
8420
marktang
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...
0
8324
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,...
0
8842
Oralloy
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...
0
8740
jinu1996
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...
1
8516
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,...
0
8617
tracyyun
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...
0
7353
agi2029
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...
0
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2743
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

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.