Last active
August 29, 2015 14:17
-
-
Save juliensimon/f9e6bcd88784c670dd8a to your computer and use it in GitHub Desktop.
This file contains 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 interface LinearSearchMode<T> { | |
public void moveElement(List<T> list, int index); | |
} | |
class LinearSearchModeMoveFirst<T> implements LinearSearchMode<T> { | |
@Override | |
public void moveElement(List<T> list, int index) { | |
// Move element to the front of the list | |
T t = list.remove(index); | |
list.add(0, t); | |
} | |
} | |
class LinearSearchModeMoveLast<T> implements LinearSearchMode<T> { | |
@Override | |
public void moveElement(List<T> list, int index) { | |
// Move element to the end of the list | |
T t = list.remove(index); | |
list.add(t); | |
} | |
} | |
class LinearSearchModeMoveUp<T> implements LinearSearchMode<T> { | |
@Override | |
public void moveElement(List<T> list, int index) { | |
// Move element up, unless it's already at the front of the list | |
if (index != 0) { | |
Collections.swap(list, index-1, index); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment