• 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

method to test if an item is in the list

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Need a method that would check to see if a word being added to a list is already in the list at the current position and if so to increment the list count but not add the word. Don't know how to do this. I have method to append the list, would it be better to write some sort of if statement for these methods instead.

in the main
L2.append(new WordElem("Mountain", 5));
want to append the list, check to see if the item that is being added is at the current position(end of the list) and if so increment the count only.
thanks anyone
[This message has been edited by Ronald Pruitt (edited January 21, 2001).]
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think from your code that you are using an ArrayList (if not please correct me).
First of all, you are sort of man-handling it to keep track of the index yourself. Normally you would just check the ArrayList with the contains() method to see if the word is aready in the array, and you would use the size() method to keep track of how many elements are in the list.
However, assuming that you have some wise and wonderful reason for tracking it yourself, the following code will work for you. I took the liberty of changing the WordElem to a String for ease of testing, and changing the full test to an if statement, and using the add() method for ArrayList:
 
Ronald Pruitt
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Cindy Glass:
[B]I think from your code that you are using an ArrayList (if not please correct me).
First of all, you are sort of man-handling it to keep track of the index yourself. Normally you would just check the ArrayList with the contains() method to see if the word is aready in the array, and you would use the size() method to keep track of how many elements are in the list.

Thanks for your help
However, assuming that you have some wise and wonderful reason for tracking it yourself, the following code will work for you. I took the liberty of changing the WordElem to a String for ease of testing, and changing the full test to an if statement, and using the add() method for ArrayList:
[/B]


 
reply
    Bookmark Topic Watch Topic
  • New Topic