-
Notifications
You must be signed in to change notification settings - Fork 8
/
DynamicArray.java
68 lines (67 loc) · 1.62 KB
/
DynamicArray.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import java.util.Arrays;
//*******************************************************
// DynamicArray.java
// A class that represents an implementation of DynamicArray in java
// Author: liron mizrahi
//*******************************************************
public class DynamicArray<T>
{
Object[] _data;
int _size;
/**
* constructor of the class
* @param None
* @return None
*/
public DynamicArray()
{
_size = 0;
_data = new Object[1];
}// end of method DynamicArray
/**
* getSize method return the size of the dynamic array
* @param None
* @return int
*/
public int getSize()
{
return _size;
}// end of method getSize
/**
* get method return the value of the arr in given index
* @param int
* @return T (generics)
*/
public T get(int index)
{
return (T) _data[index];
}// end of method get
/**
* put method insert a element to the arr
* @param int
* @return T (generics)
*/
public void put(Object element)
{
ensureCapacity(_size + 1);
_data[_size++] = element;
}// end of method put
/**
* ensureCapacity method ensure the capacity is
* @param int
* @return T (generics)
*/
public void ensureCapacity(int minCapacity)
{
int oldCapacity = getSize();
if( minCapacity > oldCapacity)
{
int newCapacity = oldCapacity * 2;
if(newCapacity < minCapacity)
{
newCapacity = minCapacity;
}
_data = Arrays.copyOf(_data, newCapacity);
}
}// end of method ensureCapacity
}// end of class DynamicArray