• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Comparing Object Elements

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi again all, I was wondering if some one could help me.

What I'm tring to do is compare elements in an ArrayList. I must use the method signature and the ArrayList stock.add(item) but the rest of the body I can change (as long as it is not to advanced yet).



Any help on pointing me in the right direction on how to compare these different object id's would be great

Thanks
Mike
 
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What errors is the compiler giving?

Does getID() really exist(check spelling) in that class and what is its visibility modifiers?

The easiest way to do this is check to see if Product implements Comparable and the compareTo() method does what you need. If it doesn't, and you can change Product, do it this way.
 
Mike Stevens
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi David the error I get is can't find symbol - method getId() but only when I try to call the local var stockString.getID(). If I take this out there is no error but I can not compare either

I'm not sure what you mean by this

The easiest way to do this is check to see if Product implements Comparable and the compareTo() method does what you need. If it doesn't, and you can change Product, do it this way.

I'm using the class Product method getID() in a number of other methods and all works fine along with some other Product methods.

I think I cannot use the Product method getID() on the local var I'm just not really sure how else to compare it.
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mike,

please have a good look at your code! You are calling the method of the object. I suspect you wanted to write , didn't you?

Regards,
David
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Does getID() really exist(check spelling) in that class and what is its visibility modifiers?


Check the ArrayList class in the java docs and see for yourself.


Don't declare variables inside loops. Otherwise, they get redeclared everytime through the loop. Instead, declare them outside the loop, and then make assignments to them inside the loop.
[ November 10, 2006: Message edited by: sven studde ]
 
David Nemeskey
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sven:

is that really so? I thought the compiler was smarter than that.
 
Mike Stevens
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by David Nemeskey:
Mike,

please have a good look at your code! You are calling the method of the object. I suspect you wanted to write , didn't you?

Regards,
David



I tried this last night and it compiled, it just never added it to the collection

with
stock.add(item);

I'm going to have a fresh look at my method and these posts later today
 
Mike Stevens
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mike Stevens:


I tried this last night and it compiled, it just never added it to the collection

with

I'm going to have a fresh look at my method and these posts later today
PS Thanks for taking a boo

 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This seems completely unneeded.
 
Mike Stevens
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tim LeMaster:


This seems completely unneeded.



It is I was just trying to pass

item
in to
stockString
to see if this would make a difference

It is commented out now

Thanks for taking a look
Mike
 
Clowns were never meant to be THAT big! We must destroy it with this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic