English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

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

4 answers

Something about your code that I noticed:

>public static void printList(List L)
>{
>for (int i=0;i this for loop actually does nothing except for incrementing i all the way.

>System.out.print(L);
It will print out reference information about object, i doubt it will be of any use to you.

>System.out.println("=======\n"...

See documentation on Java API on List interface.
int indexOf(Object o)
Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. More formally, returns the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such index.

Parameters:
o - element to search for
Returns:
the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element
Throws:
ClassCastException - if the type of the specified element is incompatible with this list (optional)
NullPointerException - if the specified element is null and this list does not permit null elements (optional)

2007-02-20 10:00:10 · answer #1 · answered by mirbeksm 2 · 0 0

The answer to this was given in your printing question.

Use a Do while not a for loop!

Use the end of the list as an exit condition with an or and a test on an integer or a boolean flag.
Set the flag to false or zero outside the loop and then when you find the match set the flag to true or 1.

2007-02-20 08:03:48 · answer #2 · answered by AnalProgrammer 7 · 0 0

Have you tried using the indexOf(Object o) method which will return the int position in the list? If the Object does not exist in the list, it will return a -1 value to your variable.

Your mySearch method should look like this (a little longer than absolutely necessary but hopefully clearer like this).

public int mySearchList(List L, String item)
{
int myIndex = 0;
try
{
// If "item" is found, the position will be returned, otherwise
// a "-1" will be returned.
myIndex = L.indexOf(item);
}
catch (ClassCastException cce)
{
// Some code to handle this error.
return -1;
}
catch (NullPointerException npe)
{
// Some code to handle this error.
return -1;
}
return myIndex;
}

2007-02-19 08:19:54 · answer #3 · answered by Jim Maryland 7 · 0 1

frequently, on a roll-your-very own LinkedList you come the Node (or null). If the outcomes return a null, then seek isn't got here upon. LinkedList purely works by using fact it quite is a regarded after dataset. i don't understand the way you wrote your classification Node, yet look over the link and simplify your technique for seek.

2016-12-17 13:59:16 · answer #4 · answered by spadafora 4 · 0 0

fedest.com, questions and answers