Question:
Need help on finding position of an element in an array (Java)?
LUX
2011-10-14 02:32:07 UTC
I have a method that accepts an object parameter and checks if it equals an element in an array and then returns that position.
My problem is that I must return an int value even when the element is not found. What can I return when the element is not found. I have already included an exception that is thrown whenever the element is not found, but the compiler complains that the returning int value might not have been initialized. Here is a sample of my code;
public int indexOf(Object element)
{
int pos;
boolean pass = false;
// returns the index of the first match of the element
// from the list by a search algorithm
for(int i = 0; i < size; i++)
{
if(list[i].equals(element))
{
pass = true;
return i;
}
}

if(pass == false)
{
throw new NoSuchElementException();
}
}
Four answers:
McFate
2011-10-14 04:12:35 UTC
It's not that you need to return an int when you want to throw an exception. It's that the compiler "sees" a path to the bottom of the method (your for-loop ends, but pass is not false) which has no valid return value and does not throw an exception. It basically sees:



for () {

if () return an int; // valid return

}



if () throw an exception; // exception thrown



// INVALID -- no return, no exception

}



Java doesn't intuit the fact that the "if" expression at the bottom of your method will always evaluate to "true" -- because "pass" can only be set to true in a block of code that returns from the method right away. There is no path out of the for-loop which has "pass" set to anything other than false. But the way the code is structured, the compiler sees a path in the code at the bottom of the method that is not compatible with the required return type. So it complains.



You can help it out by making it clear that if your for-loop ends without returning, then you know pass is false anyway. You don't need that if-statement:



} // for-loop ends

throw new NoSuchElementException();

} // method ends



Then you don't need an unreachable statement to return an int at the bottom. The compiler can see that there is no path to the bottom of the method without either a valid return value or throwing an exception.



Two other minor notes:



(1) You don't need to explicitly compare booleans to constant values, as they are typed properly to be used directly in a boolean expression. If you were keeping the "if (pass == false)" statement, it would be better written "if ( ! pass )".



(2) You don't need the value of "pass" at all. You can remove the variable entirely, since you don't do anything with the value once the if-statement is removed. Your entire method would be:



for (int i=0; i
throw new NoSuchElementException();
anonymous
2011-10-14 02:41:48 UTC
Just return -1
green meklar
2011-10-14 08:07:10 UTC
I would return -1. Since valid array indices are never negative, this is a pretty clear indication of an error.
jaideep
2017-03-03 16:20:13 UTC
you will desire to truly get used to utilising the perfect indices for you arrays. they're listed from 0. You loop to get enter is defective. Use for( int ok=0; ok


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...