Package it.unimi.di.prog2.h22
Class ArrayUtils
java.lang.Object
it.unimi.di.prog2.h22.ArrayUtils
A class providing some utility methods for arrays.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> int
countNull
(T[] vals) Counts the number ofnull
elements in an array.static <T extends Comparable<T>>
intcountSmaller
(T[] array, T element) Counts the number of elements in an array that are smaller than a given element.static <T> int
linearSearch
(T[] haystack, T needle) Searches for a needle in a haystack.static <U,
T extends U>
voidPours the elements of a list into another list.
-
Constructor Details
-
ArrayUtils
private ArrayUtils().
-
-
Method Details
-
countNull
public static <T> int countNull(T[] vals) Counts the number ofnull
elements in an array.- Type Parameters:
T
- the type of the elements in the haystack.- Parameters:
vals
- the array.- Returns:
- the number of
null
elements invals
. - Throws:
NullPointerException
- ifvals
isnull
.
-
linearSearch
public static <T> int linearSearch(T[] haystack, T needle) Searches for a needle in a haystack.Returns an index
i
such thathaystack[i] == needle
, or-1
if the needle does not appear in the haystack.- Type Parameters:
T
- the type of the elements in the haystack.- Parameters:
haystack
- the haystack.needle
- the needle.- Returns:
- an index
i
such thathaystack[i] == needle
, or-1
if the needle does not appear in the haystack. - Throws:
NullPointerException
- ifhaystack
orneedle
arenull
.
-
countSmaller
Counts the number of elements in an array that are smaller than a given element.- Type Parameters:
T
- the type of the elements in the array.- Parameters:
array
- the array.element
- the element to compare.- Returns:
- the number of elements in
array
that are smaller thanelement
. - Throws:
NullPointerException
- ifarray
isnull
.
-
pour
Pours the elements of a list into another list.- Type Parameters:
U
- the type of the elements of the destination list.T
- the type of the elements of the source list.- Parameters:
src
- the source list.dst
- the destination list.- Throws:
NullPointerException
- ifsrc
ordst
arenull
.
-