Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package vector;
- import java.util.EmptyStackException;
- import org.omg.CORBA.BAD_PARAM;
- public class vector {
- private int _size;
- private Object _Array[];
- public vector (int size, Object value) { //fill constructor
- if (size > 0) {
- _size = size;
- }
- else {
- _size = 0;
- }
- _Array = new Object[2*size + 1];
- for (int i = 0; i < _size; ++i)
- _Array[i] = value;
- }
- public vector () {
- this (0, 0);
- }
- public int size () {
- return _size;
- }
- public int capacity () {
- return _Array.length;
- }
- public boolean empty () {
- return (_size == 0);
- }
- public void reserve (int capacity) {
- if (capacity <= 0)
- throw new BAD_PARAM();
- Object Temp_array[] = new Object[capacity];
- for (int i = 0; i < _size; ++i)
- Temp_array[i] = _Array[i];
- _Array = Temp_array;
- }
- public void resize (int size, Object value) {
- if (size < _size && size >= 0) { // change _capacity?
- _size = size;
- return;
- }
- if (size > _size) {
- if (size >= _Array.length) {
- if (2*_Array.length > size)
- reserve (2*_Array.length);
- else
- reserve (size);
- }
- for (int i = _size; i < size; ++i)
- _Array[i] = value;
- _size = size;
- }
- }
- public void pushBack (Object value) {
- resize (_size + 1, value);
- }
- public void shrinkToFit () {
- if (_size != _Array.length) {
- Object Temp_array[] = new Object[_size];
- for (int i = 0; i < _size; ++i)
- Temp_array[i] = _Array[i];
- _Array = Temp_array;
- }
- }
- public Object popBack () {
- if (!empty())
- return _Array[--_size];
- throw new EmptyStackException();
- }
- public void clear () {
- _size = 0;
- Object Temp_array[] = new Object[10];
- _Array = Temp_array;
- }
- public Object at (int id) { // ?
- if (id >= _size || id < 0)
- throw new ArrayIndexOutOfBoundsException(id);
- return _Array[id];
- }
- public Object front () {
- return at(0);
- }
- public Object back () {
- return at(_size - 1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement