Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** Created by Dayu Wang (dwang@stchas.edu) on 06-07-2019. */
- /** Last updated by Dayu Wang (dwang@stchas.edu) on 06-08-2019. */
- package data_structures;
- import java.util.Arrays;
- public class Array_List<T> {
- private Object[] data;
- private int capacity, used;
- private static final int DEFAULT_CAPACITY = 10;
- public Array_List() { data = new Object[capacity = DEFAULT_CAPACITY]; }
- public Array_List(int cap) { data = new Object[capacity = cap]; }
- public Array_List(Array_List<T> other) {
- capacity = other.capacity;
- used = other.used;
- data = Arrays.copyOf(other.data, other.capacity);
- }
- public Array_List(Object[] other) {
- capacity = used = other.length;
- data = Arrays.copyOf(other, other.length);
- }
- @SuppressWarnings("unchecked")
- public T get(int index) {
- if (index < 0 || index >= used) { throw new IndexOutOfBoundsException("Index: " + index); }
- return (T)data[index];
- }
- public void set(int index, T value) {
- if (index < 0 || index >= used) { throw new IndexOutOfBoundsException("Index: " + index); }
- data[index] = value;
- }
- private void reserve() { data = Arrays.copyOf(data, capacity *= 2); }
- public void add(T value) {
- if (capacity == used) { reserve(); }
- data[used++] = value;
- }
- public void insert(int index, T value) {
- if (index < 0 || index > used) { throw new IndexOutOfBoundsException("Index: " + index); }
- if (capacity == used) { reserve(); }
- for (int i = used - 1; i >= index; i--) { data[i + 1] = data[i]; }
- data[index] = value;
- used++;
- }
- @SuppressWarnings("unchecked")
- public T remove(int index) {
- if (index < 0 || index >= used) { throw new IndexOutOfBoundsException("Index: " + index); }
- T value = (T)data[index];
- for (int i = index + 1; i < used; i++) { data[i - 1] = data[i]; }
- used--;
- return value;
- }
- @SuppressWarnings("unchecked")
- public T remove() throws Exception {
- if (used == 0) { throw new Exception("Attempt to remove item from empty array list."); }
- return (T)data[used--];
- }
- public boolean isEmpty() { return used == 0; }
- public int size() { return used; }
- public boolean contains(T item) {
- for (int i = 0; i < used; i++) {
- if (item.equals(data[i])) { return true; }
- }
- return false;
- }
- public int indexOf(T item) {
- for (int i = 0; i < used; i++) {
- if (item.equals(data[i])) { return i; }
- }
- return -1;
- }
- public int lastIndexOf(T item) {
- for (int i = used - 1; i >= 0; i--) {
- if (item.equals(data[i])) { return i; }
- }
- return -1;
- }
- public void trimToSize() { data = Arrays.copyOf(data, capacity = used); }
- public Object[] toArray() { return Arrays.copyOf(data, used); }
- public void clear() { used = 0; }
- @Override
- public String toString() {
- String output = "[";
- for (int i = 0; i < used; i++) {
- output += data[i].toString();
- if (i != used - 1) { output += ", "; }
- else { output += ']'; }
- }
- return output;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement