• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

"-1 if this list does not contain the element." problem using arraylist

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all, I am facing this problem where I am unable to pass the index value of an element in an array list. currently i am using 2 classes.. Show and Main(using one method findShow)
I am unable to put in the index value in a variable called locationIndex.

System.out.println(locationIndex); //This would always show "-1" for output




The only clue i got is that "-1 if this list does not contain the element."
Any help or comment on this would be great!

Regards,
Jon
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Before going any further, I'm a bit concerned by Video(int showid, String showtitle, String showreview) in the Show class.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It will be difficult to help without knowing what listVideos is.
 
Jon Kho
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
opss, i have just updated the codes... it's actually listShows which is from listShows = new ArrayList();
in fact i used this example from http://www.idevelopment.info as a guide line...


Thanks Christophe .
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

listShows contains objects of type Show (it would be useful to declare it like List<Show>).
With indexOf, you should pass a Show object, not an integer. That's why you always get -1.
 
Jon Kho
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you mean like this?

or
where <Show> is a "Show"class.

as for passing of a Show object, can you give me an example of it? My guess would be using s1.toString() as an object but i tried it and it still returns me -1 output...

info on the toString Method that i have used..


Thanks
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, List.indexOf uses the equals method to find the object. Since you haven't overridden that, it still uses Object's equals method - which only uses == for checking.

Now that may be what you want, but I thought I'd warn you to prevent future problems.
 
Jon Kho
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:Also, List.indexOf uses the equals method to find the object. Since you haven't overridden that, it still uses Object's equals method - which only uses == for checking.

Now that may be what you want, but I thought I'd warn you to prevent future problems.



Hi Rob,
I am just curious about the future problem which you have mentioned. is it because that it uses the function of "checking" and not "searching"? in actually check I prefer to use the "search" function. In fact i came across this method, from here.

Is it better to use this one than the one i am currently using?

Thanks for your concern.

Jon
 
Jon Kho
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anyway, i managed to got what i wanted. I replace "s" to "s1" where s1 contains the object attributes so i do not need to use the toString Method..

However, this code is incomplete as it lacks of If Else then functions which i am going to edit later in the day... In the meanwhile, i appreciate any comments to improve on this coding showed..

Thanks for the help guys.
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jon Kho wrote:

Rob Prime wrote:Also, List.indexOf uses the equals method to find the object. Since you haven't overridden that, it still uses Object's equals method - which only uses == for checking.

Now that may be what you want, but I thought I'd warn you to prevent future problems.



Hi Rob,
I am just curious about the future problem which you have mentioned. is it because that it uses the function of "checking" and not "searching"? in actually check I prefer to use the "search" function. In fact i came across this method, from here.

Is it better to use this one than the one i am currently using?

Thanks for your concern.

Jon


indexOf uses the equals method, and Collections.binarySearch uses the compareTo method (or a custom comparator). Now String has great implementations for both, but if you want to search a List<Show> and return an element, you have two options:
- provide the same object reference as is already in the list
- override equals

If you don't override equals, the following will always print -1:
That's because equality is based on reference equality.

You can do a search to find good examples of equals methods. Don't forget to override hashCode() as well.

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic