Skip to content

Instantly share code, notes, and snippets.

@xSAVIKx
Created December 19, 2016 17:28
Show Gist options
  • Save xSAVIKx/3c4e961bbd65052ec573fe8918f09052 to your computer and use it in GitHub Desktop.
Save xSAVIKx/3c4e961bbd65052ec573fe8918f09052 to your computer and use it in GitHub Desktop.
package school.lemon.changerequest.java.pr1.lesson;
import java.util.Arrays;
/**
* Created by User on 19.12.2016.
*/
public class Array {
private int[] array;
private int size;
public Array(int size) {
this.array = new int[size];
}
public void add(int element) throws ArrayIsFullException {
if (size == array.length) {
throw new ArrayIsFullException(String.format("Size of array is %d", array.length));
}
array[size++] = element;
}
public int get(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException(
String.format("You are trying to get element at non-existing index %d", index));
}
return array[index];
}
@Override
public String toString() {
return "Array{" +
"array=" + Arrays.toString(array) +
", size=" + size +
'}';
}
private static class ArrayIsFullException extends Exception {
public ArrayIsFullException() {
}
public ArrayIsFullException(String message) {
super(message);
}
public ArrayIsFullException(String message, Throwable cause) {
super(message, cause);
}
public ArrayIsFullException(Throwable cause) {
super(cause);
}
public ArrayIsFullException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment