Util: Add ArraySet.get() and new constructor

This commit is contained in:
zzz
2023-01-29 13:27:51 -05:00
parent 1e35f940da
commit 8b0cd9e36b

View File

@@ -85,6 +85,26 @@ public class ArraySet<E> extends AbstractSet<E> implements Set<E> {
addAll(c);
}
/**
* A fixed capacity of arr.length.
* Adds over capacity will throw a SetFullException.
* arr must not contain duplicates, no checks are done.
* arr may contain nulls but they must be at the end.
*
* @since 0.9.58
*/
public ArraySet(E[] arr) {
_entries = arr;
int i;
for (i = 0; i < arr.length; i++) {
if (arr[i] == null)
break;
}
_size = i;
_throwOnFull = true;
}
/**
* Adds over capacity will throw a SetFullException.
*
@@ -209,6 +229,17 @@ public class ArraySet<E> extends AbstractSet<E> implements Set<E> {
return _size;
}
/**
* @throws IndexOutOfBoundsException
* @since 0.9.58
*/
@SuppressWarnings("unchecked")
public E get(int index) {
if (index < 0 || index > _size - 1)
throw new IndexOutOfBoundsException();
return (E) _entries[index];
}
/**
* Supports remove.
* Supports comodification checks.