Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

method to test if an item is in the list

 
Ronald Pruitt
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • 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).]
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • 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
  • 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]

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic