Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. /** Created by Dayu Wang (dwang@stchas.edu) on 06-07-2019. */
  2.  
  3. /** Last updated by Dayu Wang (dwang@stchas.edu) on 06-08-2019. */
  4.  
  5. package data_structures;
  6.  
  7. import java.util.Arrays;
  8.  
  9. public class Array_List<T> {
  10. private Object[] data;
  11. private int capacity, used;
  12. private static final int DEFAULT_CAPACITY = 10;
  13. public Array_List() { data = new Object[capacity = DEFAULT_CAPACITY]; }
  14. public Array_List(int cap) { data = new Object[capacity = cap]; }
  15. public Array_List(Array_List<T> other) {
  16. capacity = other.capacity;
  17. used = other.used;
  18. data = Arrays.copyOf(other.data, other.capacity);
  19. }
  20. public Array_List(Object[] other) {
  21. capacity = used = other.length;
  22. data = Arrays.copyOf(other, other.length);
  23. }
  24. @SuppressWarnings("unchecked")
  25. public T get(int index) {
  26. if (index < 0 || index >= used) { throw new IndexOutOfBoundsException("Index: " + index); }
  27. return (T)data[index];
  28. }
  29. public void set(int index, T value) {
  30. if (index < 0 || index >= used) { throw new IndexOutOfBoundsException("Index: " + index); }
  31. data[index] = value;
  32. }
  33. private void reserve() { data = Arrays.copyOf(data, capacity *= 2); }
  34. public void add(T value) {
  35. if (capacity == used) { reserve(); }
  36. data[used++] = value;
  37. }
  38. public void insert(int index, T value) {
  39. if (index < 0 || index > used) { throw new IndexOutOfBoundsException("Index: " + index); }
  40. if (capacity == used) { reserve(); }
  41. for (int i = used - 1; i >= index; i--) { data[i + 1] = data[i]; }
  42. data[index] = value;
  43. used++;
  44. }
  45. @SuppressWarnings("unchecked")
  46. public T remove(int index) {
  47. if (index < 0 || index >= used) { throw new IndexOutOfBoundsException("Index: " + index); }
  48. T value = (T)data[index];
  49. for (int i = index + 1; i < used; i++) { data[i - 1] = data[i]; }
  50. used--;
  51. return value;
  52. }
  53. @SuppressWarnings("unchecked")
  54. public T remove() throws Exception {
  55. if (used == 0) { throw new Exception("Attempt to remove item from empty array list."); }
  56. return (T)data[used--];
  57. }
  58. public boolean isEmpty() { return used == 0; }
  59. public int size() { return used; }
  60. public boolean contains(T item) {
  61. for (int i = 0; i < used; i++) {
  62. if (item.equals(data[i])) { return true; }
  63. }
  64. return false;
  65. }
  66. public int indexOf(T item) {
  67. for (int i = 0; i < used; i++) {
  68. if (item.equals(data[i])) { return i; }
  69. }
  70. return -1;
  71. }
  72. public int lastIndexOf(T item) {
  73. for (int i = used - 1; i >= 0; i--) {
  74. if (item.equals(data[i])) { return i; }
  75. }
  76. return -1;
  77. }
  78. public void trimToSize() { data = Arrays.copyOf(data, capacity = used); }
  79. public Object[] toArray() { return Arrays.copyOf(data, used); }
  80. public void clear() { used = 0; }
  81. @Override
  82. public String toString() {
  83. String output = "[";
  84. for (int i = 0; i < used; i++) {
  85. output += data[i].toString();
  86. if (i != used - 1) { output += ", "; }
  87. else { output += ']'; }
  88. }
  89. return output;
  90. }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement