Hey guys I was just curious as to how (if) you could use linearsearch to find two of the same item in an array. For example:
I realize this will only give me the first occurence of the object, but I wasn't sure of how I'd start to look for the second (if it exists).
Code:
public static Comparable linearSearch (Comparable[] list, Comparable target)
{
int index = 0;
boolean found = false;
while (!found && index < list.length)
{
if (list[index].equals(target))
found = true;
else
index++;
}
if (found)
return list[index];
else
return null;
}
Comment