Advertisement
Guest User

Untitled

a guest
Mar 5th, 2015
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.13 KB | None | 0 0
  1. package vector;
  2.  
  3. public class vector {
  4.     private int _capacity;
  5.     private int _size;
  6.     private int _Array[];
  7.     private int ERROR = -2147483648;
  8.  
  9.  
  10.     public vector () { //default constructor
  11.         _capacity = 10;
  12.         _size = 0;
  13.         _Array = new int[_capacity];
  14.     }
  15.     public vector (int size, int value) { //fill constructor
  16.         if (size > 0) {
  17.             _capacity = 2*size;
  18.             _size = size;
  19.         }
  20.         else {
  21.             _size = 0;
  22.             _capacity = 1;
  23.         }
  24.         _Array = new int[_capacity];
  25.         for (int i = 0; i < _size; ++i)
  26.             _Array[i] = value;
  27.     }
  28.  
  29.     public int size () {
  30.         return _size;
  31.     }
  32.  
  33.     public int capacity () {
  34.         return _capacity;
  35.     }
  36.  
  37.     public boolean empty () {
  38.         return (_size == 0);
  39.     }
  40.  
  41.     public void reserve (int capacity) {
  42.         if (capacity <= 0)
  43.             return;
  44.  
  45.         int Temp_array[] = new int[capacity];
  46.         for (int i = 0; i < _size; ++i) {
  47.             Temp_array[i] = _Array[i];
  48.         }
  49.         _Array = Temp_array;
  50.         _capacity = capacity;
  51.     }
  52.  
  53.     public void resize (int size, int value) {
  54.         if (size < _size && size >= 0) { // zmieniac _capacity?
  55.             _size = size;
  56.             return;
  57.         }
  58.  
  59.         if (size > _size) {
  60.             if (size >= _capacity) {
  61.                 if (2*_capacity > size)
  62.                     reserve (2*_capacity);
  63.                 else
  64.                     reserve (size);
  65.             }
  66.  
  67.             for (int i = _size; i < size; ++i)
  68.                 _Array[i] = value;
  69.  
  70.             _size = size;
  71.         }
  72.     }
  73.  
  74.     public void push_back (int value) {
  75.         resize (_size + 1, value);
  76.     }
  77.  
  78.  
  79.     public void shrink_to_fit () {
  80.         if (_size != _capacity) {
  81.             int Temp_array[] = new int[_size];
  82.             for (int i = 0; i < _size; ++i) {
  83.                 Temp_array[i] = _Array[i];
  84.             }
  85.             _Array = Temp_array;
  86.             _capacity = _size;
  87.         }
  88.     }
  89.  
  90.     public int pop_back () {
  91.         if (!empty())
  92.             return _Array[--_size];
  93.  
  94.         return ERROR;
  95.     }
  96.  
  97.     public void print () {
  98.         for (int i = 0; i < _size; ++i) {
  99.             System.out.println(_Array[i] + " ");
  100.         }
  101.     }
  102.  
  103.     public void clear () {
  104.         _capacity = 10;
  105.         _size = 0;
  106.         int Temp_array[] = new int[10];
  107.         _Array = Temp_array;
  108.     }
  109.  
  110.     //  public int & at (int id) { // ?
  111.     //      return _Array[id];
  112.     //  }
  113.     // 
  114.     //  public int & front () { // ?
  115.     //      return _Array[0];
  116.     //  }
  117.     // 
  118.     //  public int & back () { // ?
  119.     //      return _Array[_size];
  120.     //  }
  121.  
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement