Advertisement
Guest User

Untitled

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