-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUniversalArray.java
More file actions
45 lines (35 loc) · 1.13 KB
/
UniversalArray.java
File metadata and controls
45 lines (35 loc) · 1.13 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
package com.yurii.salimov.lesson08.task03;
import java.util.Arrays;
/**
* @author Yuriy Salimov (yuriy.alex.salimov@gmail.com)
* @version 1.0
*/
public final class UniversalArray<T extends Number> {
private final T[] array;
public UniversalArray(final T[] array) {
this.array = array;
}
@Override
public String toString() {
return "UniversalArray{array=" + Arrays.toString(this.array) + '}';
}
public T[] getArray() {
return this.array;
}
public T getElement(final int index) throws ArrayIndexOutOfBoundsException {
checkIndex(index);
return this.array[index];
}
public void setElement(final T value, final int index) throws ArrayIndexOutOfBoundsException {
checkIndex(index);
this.array[index] = value;
}
public int size() {
return this.array.length;
}
private void checkIndex(final int index) throws ArrayIndexOutOfBoundsException {
if ((index < 0) || (index >= this.array.length)) {
throw new ArrayIndexOutOfBoundsException("Index: " + index + ", Size: " + this.array.length);
}
}
}