Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package vector;
- public class vector {
- private int _capacity;
- private int _size;
- private int _Array[];
- private int ERROR = -2147483648;
- public vector () { //default constructor
- _capacity = 10;
- _size = 0;
- _Array = new int[_capacity];
- }
- public vector (int size, int value) { //fill constructor
- if (size > 0) {
- _capacity = 2*size;
- _size = size;
- }
- else {
- _size = 0;
- _capacity = 1;
- }
- _Array = new int[_capacity];
- for (int i = 0; i < _size; ++i)
- _Array[i] = value;
- }
- public int size () {
- return _size;
- }
- public int capacity () {
- return _capacity;
- }
- public boolean empty () {
- return (_size == 0);
- }
- public void reserve (int capacity) {
- if (capacity <= 0)
- return;
- int Temp_array[] = new int[capacity];
- for (int i = 0; i < _size; ++i) {
- Temp_array[i] = _Array[i];
- }
- _Array = Temp_array;
- _capacity = capacity;
- }
- public void resize (int size, int value) {
- if (size < _size && size >= 0) { // zmieniac _capacity?
- _size = size;
- return;
- }
- if (size > _size) {
- if (size >= _capacity) {
- if (2*_capacity > size)
- reserve (2*_capacity);
- else
- reserve (size);
- }
- for (int i = _size; i < size; ++i)
- _Array[i] = value;
- _size = size;
- }
- }
- public void push_back (int value) {
- resize (_size + 1, value);
- }
- public void shrink_to_fit () {
- if (_size != _capacity) {
- int Temp_array[] = new int[_size];
- for (int i = 0; i < _size; ++i) {
- Temp_array[i] = _Array[i];
- }
- _Array = Temp_array;
- _capacity = _size;
- }
- }
- public int pop_back () {
- if (!empty())
- return _Array[--_size];
- return ERROR;
- }
- public void print () {
- for (int i = 0; i < _size; ++i) {
- System.out.println(_Array[i] + " ");
- }
- }
- public void clear () {
- _capacity = 10;
- _size = 0;
- int Temp_array[] = new int[10];
- _Array = Temp_array;
- }
- // public int & at (int id) { // ?
- // return _Array[id];
- // }
- //
- // public int & front () { // ?
- // return _Array[0];
- // }
- //
- // public int & back () { // ?
- // return _Array[_size];
- // }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement