-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP03.java
More file actions
33 lines (28 loc) · 698 Bytes
/
P03.java
File metadata and controls
33 lines (28 loc) · 698 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package lists;
import java.util.List;
import java.util.NoSuchElementException;
/**
* Class for finding the K-th element in list.
*/
final class P03 {
private P03() {
}
/**
* Find the k-th item in list.
*
* @param numbers list of items
* @param i index of the item
* @param <T> type of item
* @return item
*/
public static <T> T kth(final List<T> numbers, final int i) {
if (numbers.isEmpty()) {
throw new NoSuchElementException("Empty list");
}
if (i >= 0) {
return numbers.get(i);
} else {
throw new NoSuchElementException("Invalid index");
}
}
}