The question is pretty much as stated in the title. I'm in an algorithms course and the professor and I disagree regarding whether or not operations performed on an ArrayList sublist (a sublist generated by ArrayList.sublist) can be considered 'in place'. To my read of the Java API:
Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive. (If fromIndex and toIndex are equal, the returned list is empty.) The returned list is backed by this list, so non-structural changes in the returned list are reflected in this list, and vice-versa. The returned list supports all of the optional list operations.
you are still manipulating the 'master' ArrayList directly. To his view, you are copying references from the 'master' array into a new sub-array which means employing ArrayList.subList is not considered 'in place'. Obviously, for the purposes of the course what he says goes (that is, if I want to pass :-/) but I would like to know either way for my own growth as a programmer. Code is below - and thank you!
public static int findK (int findME, int mVal, ArrayList<Integer> arr) {
// pre stage return variable
int returnVal = -1;
// make a subarray consisting of indexes 0 - ((m-2)+(m-1)).
// this because the relationship between the m, p, and q
// is p>q>m - therfore the max value of q is m-1 and the
// max value of p is m-2.
int newArrSize = (mVal-2) + (mVal-1);
ArrayList<Integer> subArr = new ArrayList<Integer>(arr.subList(0, newArrSize));
// make the list smaller by looking at only the last [mVal]
// elements. this because we know what we're looking for
// has to be in the second section of the array, and that
// section can't possibly be larger than mVal
int fromIndex = subArr.size() - mVal;
subArr = new ArrayList<Integer> (subArr.subList(fromIndex, subArr.size()));
// at this point we can do a simple binary search, which on an
// a sorted array of size mVal is lg(m)
while (subArr.size() > 1) {
// get midpoint value
int midPointIndex = subArr.size() / 2;
int midPointValue = subArr.get(midPointIndex);
// check for case where midpoint value is in the first
// region of the array
// check for case where the midpoint is less than the
// findME value
//
// if true, discard first half of the array
if ((midPointValue == 9000) || (midPointValue < findME)) {
subArr = new ArrayList<Integer> (subArr.subList(midPointIndex, subArr.size()));
continue;
}
// else if midpoint is less than findMe, discard the second
// half of the array
else if (midPointValue > findME) {
subArr = new ArrayList<Integer> (subArr.subList(0, midPointIndex));
continue;
}
// if we're here, we've found our value!
returnVal = midPointValue;
break;
}
// check for match and return result to caller
// only perform check if we haven't already found the value
// we're looking for
if (returnVal == -1) returnVal = (subArr.get(0) == findME) ? (subArr.get(0)) : (-1);
return returnVal;
}
ArrayList<Integer>no matter what you do with it, and that test doesn't even have much to do with the issue at hand anyways. I'd just direct them to the source code, as nowhere in there can they prove that a new list is made. All that's made is a new view. The abundance ofArrayList.this.___andparent.___only show that the sublist delegates all work to the containing class.