Created
January 16, 2013 05:49
-
-
Save isaacsanders/4544971 to your computer and use it in GitHub Desktop.
Any thoughts on making this even more awful?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static int search(Integer[] arriArrayToBeSearched, Integer iItemToBeFoundInArray){ | |
int iIndexOfItemToBeFoundInArrayInArrayToBeSearched = -1; | |
int iLengthOfArrayToBeSearched = 0; | |
int iTemporaryVariable = arriArrayToBeSearched.length; | |
while (iTemporaryVariable > 0) { | |
iLengthOfArrayToBeSearched += 1; | |
iTemporaryVariable -= 1; | |
} | |
for (int indexInArrayToBeSearched = 0; indexInArrayToBeSearched < iLengthOfArrayToBeSearched; indexInArrayToBeSearched += 1) { | |
Integer iMemberOfArrayToBeSearchedAtCurrentIndex = arriArrayToBeSearched[indexInArrayToBeSearched]; | |
boolean bMemberOfArrayToBeSearchedAtCurrentIndexIsEqualToTheItemToBeFoundInArray = iMemberOfArrayToBeSearchedAtCurrentIndex.equals(iItemToBeFoundInArray); | |
if (bMemberOfArrayToBeSearchedAtCurrentIndexIsEqualToTheItemToBeFoundInArray) { | |
iIndexOfItemToBeFoundInArrayInArrayToBeSearched = indexInArrayToBeSearched; | |
} | |
} | |
return iIndexOfItemToBeFoundInArrayInArrayToBeSearched; | |
} |
You can move indexInArrayToBeSearched out of the for(), Make a int iNextIndexTobeSearched outside the loop, assign it at the top of the loop to indexToBeSearched + 1, and in the last clause of the for(;;), set indexInArrayToBeSearched = nextIndexToBeSearched.
Also, you can special case with if (arriArrayToBeSearched.length == 0) { iIndexOfitemToBeFoundInArrayInArrayToBeSearched = -1; } else { ... the rest of algorithm ... }
Or you could put
if (iItemToBeFoundInArray + arriArrayToBeSearched.length == iItemToBeFoundInArray)
return -1;
At the beginning.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You could replace those numbers with constants, such as line 3:
int iLengthOfArrayToBeSearched = INITIAL_LENGTH_OF_ARRAY_TO_BE_SEARCHED;