Between our exploration of arrays, ArrayList
, and LinkedLink
in this module and our look at some common functions in our look at worst case analysis in the previous module, we’ve accumulated a fair number of “utility” functions for doing things like searching and inserting into sequences.
A number of these are already available to us as Java programmers, however, via the classes java.util.Arrays
and java.util.Collections
.
You should become familiar with some of these so that you avoid “reinventing the wheel” when writing your own code.
java.util.Arrays
provides utility functions for working with ordinary arrays.java.util.Collections
provides utilities for working with List
and other Java collections.We have previously discussed the sequential search algorithm.
If you aren’t working with arrays, the List
class provides the indexOf
function that implements a sequential search:
List<String> names = ...;
⋮
int k = names.indexOf(myName);
if (k >= 0) {
System.out.println("myName is in the list at position " + k);
} else {
System.out.println("myName is not in the list.");
}
list.indexOf(data)
is O(list.size()).
As far as I know, there is no built-in sequential search function for arrays.
We have seen that, if our data is sorted, then binary search can be much faster than sequential.
This is available both for arrays:
String[] names = new String[N];
⋮
int k = java.util.Arrays.binarySearch(names, myName);
The above call is O(lognames.length).
and for List
s:
List<String> names = ...
⋮
int k = java.util.Collections.binarySearch(names, myName);
The above call is O(lognames.size()) if
names
is anArrayList
, but is O(names.size()) ifnames
is anLinkedLost
.
Arrays
form of binary search has variations that allow you to supply a starting and ending position within the array, useful if the array is actually longer than the amount of data it holds.Collections
form of binary search has variations that allow you to supply a Comparator
to compare elements if needed.You can use an import
statement to shorten the function names.
import java.util.Arrays;
import java.util.Collections;
This would allow you to write simple Arrays.binarySearch
and Collections.binarySearch
.
You can shorten things even further to just say binarySearch
with
import static java.util.Arrays.binarySearch;
import static java.util.Collections.binarySearch;
All of the functions in Arrays
and Collections
are static
, and that needs to be stated explicitly in the import.
You can exchange elements in a list like this:
Collections.swap(names, i, j);
This is O(1) if the list is an
ArrayList
, but O(list.size()) forLinkedList
s.
or you can sort them into order:
Collections.sort(names);
You can also sort arrays:
Arrays.sort(array);
You can convert between arrays and lists:
List<String> list = ...
String[] array = ...
List<String> list2 = Arrays.asList(array);
String[] array2 = list.toArray(new String[0]); // Not a Collections function.
toArray
function is a bit odd, but necessary because toArray
is actually a generic and needs to know what type of array to produce.asList
function is actually quite flexible. You cna use it with individual elements as well as with an array. E.g.,
List<String> vowels = Arrays.asList("A", "E", "I", "O", "U");
The list returned by asList
is neither ArrayList
nor LinkedList
.