GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes method to test if an item is in the list Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "method to test if an item is in the list" Watch "method to test if an item is in the list" New topic
Author

method to test if an item is in the list

Ronald Pruitt
Ranch Hand

Joined: Jan 15, 2001
Posts: 32
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

Joined: Sep 29, 2000
Posts: 8521
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:


"JavaRanch, where the deer and the Certified play" - David O'Meara
Ronald Pruitt
Ranch Hand

Joined: Jan 15, 2001
Posts: 32
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]

 
GeeCON Prague 2014
 
subject: method to test if an item is in the list