T
- element typepublic class Cons<T> extends Object
Modifier and Type | Field and Description |
---|---|
T |
car
The head of the list.
|
Cons<T> |
cdr
The tail of the list.
|
Constructor and Description |
---|
Cons(T car)
Constructs a new list consisting of a single element.
|
Cons(T car,
Cons<T> cdr)
Constructs a new cons list.
|
Modifier and Type | Method and Description |
---|---|
static <T> Cons<T> |
concatenate(Cons<T> left,
Cons<T> right)
Returns a cons list obtained by concatenating
left and right . |
static <T> Cons<T> |
drop(Cons<T> cons,
int num)
Returns the list
cons without the first num elements. |
boolean |
equals(Object o)
Returns
true iff this cons list is equal to the object o . |
static <T> Cons<T> |
flatten(Cons<Cons<T>> original)
Returns a cons list obtained by flattening the list of lists
original . |
static <T> Cons<T> |
fromArgs(T... elems)
Creates a new cons list from the given arguments.
|
static <T> Cons<T> |
fromArray(T[] elems)
Creates a new cons list from the contents of the array
elems . |
static <T> Cons<T> |
fromList(List<T> list)
Creates a new cons list from the list
list . |
static <T> T |
get(Cons<T> cons,
int idx)
Returns the
idx -th element in the cons list cons , or null
if there is no element with such an index in cons . |
int |
hashCode()
Returns the hash code of this cons list.
|
static int |
length(Cons<?> cons)
Returns the length of the cons list
cons . |
static <T> Iterator<T> |
newIterator(Cons<T> cons)
Returns a new iterator on the cons list
cons . |
Cons<T> |
prepend(Cons<T> prefix)
Prepend the list
prefix to this cons list. |
static String |
toString(Cons<?> cons,
String separator)
Returns the string representation of the list
cons , using the toString()
method to convert each element of cons to string and inserting separator
between each pair. |
public final T car
public Cons(T car, Cons<T> cdr)
car
- head of the list, must not be null
cdr
- tail of the list, may be null
NullPointerException
- if car
is null
public Cons(T car)
car
- the singleton element of the list, must not be null
public static <T> Cons<T> fromArray(T[] elems)
elems
.T
- element typeelems
- array to build the list from, must not be null
elems
(possibly null
)NullPointerException
- if elems
is null
, or any element
in elems
is null
@SafeVarargs public static <T> Cons<T> fromArgs(T... elems)
T
- element typeelems
- elements to build the list from, must not be null
elems
(possibly null
NullPointerException
- if elems
is null
, or any element
in elems
is null
public static <T> Cons<T> fromList(List<T> list)
list
.T
- element typelist
- list to build the cons list from, must not be null
elems
(possibly null
)NullPointerException
- if list
is null
, or any element
in list
is null
public static String toString(Cons<?> cons, String separator)
cons
, using the toString()
method to convert each element of cons
to string and inserting separator
between each pair.cons
- the cons list, may be null
(i.e., empty)separator
- the separator, must not be null
cons
NullPointerException
- if separator
is null
public boolean equals(Object o)
true
iff this cons list is equal to the object o
.
This cons list is equal to o
iff o
is a cons list of the same
length as this cons list, and the elements of this cons list and o
are pairwise equal (using equals(Object)
).
public int hashCode()
Note that this method traverses the entire list, and its time complexity is therefore O(n) with respect to the number of elements in the list.
public Cons<T> prepend(Cons<T> prefix)
prefix
to this cons list.
The time and space complexity of this method is O(m), where m is the
number of elements in prefix
.
prefix
- cons list to prepend to this cons list, may be null
(i.e., empty)prefix
prependedpublic static int length(Cons<?> cons)
cons
.
The time complexity of this method is O(n), where n is the
number of elements in cons
.
cons
- cons list to get the length of, may be null
(i.e., empty)cons
public static <T> T get(Cons<T> cons, int idx)
idx
-th element in the cons list cons
, or null
if there is no element with such an index in cons
.
The time complexity of this method is O(idx
).
T
- element typecons
- the cons list, may be null
(i.e., empty)idx
- the index, may be any valueidx
-th element of cons
, or null
if no such
element existspublic static <T> Cons<T> drop(Cons<T> cons, int num)
cons
without the first num
elements.
The time complexity of this method is O(idx
).
T
- element typecons
- the cons list, may be null
(i.e., empty)num
- the number of elements to drop, must be non-negativecons
without the first num
elementsIllegalArgumentException
- if num
is negativepublic static <T> Cons<T> concatenate(Cons<T> left, Cons<T> right)
left
and right
.
The time and space complexity of this method is O(m), where m is the
number of elements in left
.
T
- element typeleft
- the left list, may be null
(i.e., empty)right
- the right list, may be null
(i.e., empty)left
followed by right
public static <T> Iterator<T> newIterator(Cons<T> cons)
cons
.T
- element typecons
- the cons list, may be null
(i.e., empty)cons
Copyright © 2016. All rights reserved.