Hi i am writing a java program to search a list of names. I need to write a method to search for a supplied list L for a supplied string item, and return the position in the list where item was found (if present) or return -1 if item did not occur in L. I want to use a for loop to go though the whole list, but if i find item part of the way through i can immediately do a return with the position where i found it. This will exit the for loop (and the method) without going through to the end of the list. If i reach the end of the for loop i know i didnt find item anywhere in the list - so i want to return the value -1. I know to access an item at position I in a list of strings L, i do this by (String) L.get (i) but dont know where to put it. I will include my program so far in the extra detail as not enough room here.
2007-02-19
08:09:33
·
4 answers
·
asked by
Princess Peach
3
in
Computers & Internet
➔ Programming & Design
import java.util.*;
class pracB3a
{
public static void printList(List L)
{
for (int i=0;i
System.out.print(L);
System.out.println("=======\n");
}
public static void main(String[] args)
{
List names = new LinkedList();
names.add("Jones, M ");
names.add("Smith, J ");
names.add("Young, M ");
names.add("Bloggs, F ");
names.add("Thompson, A ");
names.add("McCullough, G");
names.add("Ling, Y ");
printList(names);
}
public static int mySearchList(List L, String item)
{
for(int i=0;i
}
}
2007-02-19
08:09:46 ·
update #1