ArrayListSearchTest - else...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
import java.util.ArrayList; import java.util.Iterator; i public class ArrayListSearchTest { public static void main (String [] arg) { System.out.println("\nArrayListTest \n"); ArrayList<String> aList = new ArrayList<String>(); aList.add("Mary"); aList.add("James"); aList.add("Kevin"); System.out.println("aList contains:"); Iterator it = aList.iterator(); while (it.hasNext()) { System.out.println(; } int foundPosition = searchArray (aList,"Mary" ); if (foundPosition != -1) System.out.println("found in position " + foundPosition);
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: else System.out.println(&quot;not found&quot;); S } public static int searchArray(ArrayList&lt;String&gt; arrList, String name) { int i = 0, position = -1; //iterate through the list until found or at the end boolean found = false; //Iterator it = arrList.iterator(); while ( !found &amp;&amp; (i &lt; arrList.size()) ) { System.out.println(&quot;i is &quot; + i); if (arrList.get(i).equalsIgnoreCase(name)) { position = i; found = true; } i++; } //end while() return position; } }...
View Full Document

This note was uploaded on 06/07/2010 for the course CS 201 taught by Professor Staff during the Spring '08 term at Iona.

Ask a homework question - tutors are online