473,326 Members | 2,136 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Increment object member in an Array List

dlite922
1,584 Expert 1GB
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 8699
dlite922
1,584 Expert 1GB
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 8TB
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 Expert 1GB
@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
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...
16
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) ){...
3
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::*...
3
by: jut_bit_zx | last post by:
class A { public: A(); virtual ~A(){} .... private: int m_iarray; }
4
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
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...
4
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...
4
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
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?...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.