Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.37 KB | None | 0 0
  1. package com.t3.datastructures.lists;
  2.  
  3.  
  4. import java.sql.Array;
  5. import java.util.Arrays;
  6.  
  7. /**
  8. * Created by Tony Nguyen on 24.03.2017.
  9. */
  10. public class ArrayList<T> {
  11.  
  12. class ArrayListItem<T> {
  13. public ArrayListItem<T> next;
  14. // n is the amount of elements in the Array A
  15. public int n;
  16. public T[] A;
  17.  
  18. public ArrayListItem(T key, int N) {
  19. this.n = 1;
  20. this.A = (T[]) new Object[N];
  21. this.A[0] = key;
  22. this.next = null;
  23. }
  24.  
  25. public ArrayListItem(int N) {
  26. this.n = 0;
  27. this.A = (T[]) new Object[N];
  28. this.next = null;
  29. }
  30. }
  31.  
  32. private ArrayListItem<T> head;
  33. // N is the length of each Array of the ArrayListItems
  34. private int N;
  35.  
  36. public ArrayList(int N) {
  37. this.N = N;
  38. head = new ArrayListItem<T>(N);
  39. }
  40.  
  41. public void add(T key) {
  42. ArrayListItem<T> p = head;
  43.  
  44. // Array of the item has no elements
  45. if (p.n != this.N) {
  46. p.A[p.n] = key;
  47. p.n++;
  48. } else {
  49. while (p.n == this.N) {
  50. if (p.next != null) {
  51. p = p.next;
  52. } else {
  53. p.next = new ArrayListItem<T>(key, this.N);
  54. return;
  55. }
  56. }
  57. p.A[p.n] = key;
  58. p.n++;
  59. }
  60. }
  61.  
  62. public void addAt(int index, T key) {
  63.  
  64. }
  65.  
  66. public boolean remove(T key) {
  67. ArrayListItem<T> p = head;
  68. int index = 0;
  69.  
  70. while (p.A[index] != key) {
  71. if (index + 1 == N) {
  72. if (p.next == null) {
  73. return false;
  74. } else {
  75. p = p.next;
  76. index = 0;
  77. }
  78. } else {
  79. index++;
  80. }
  81. }
  82. for (int i = index; i < N-1; i++) {
  83. p.A[i] = p.A[i + 1];
  84. }
  85. p.n--;
  86. return true;
  87. }
  88.  
  89. public T get(int i) {
  90. ArrayListItem<T> p = head;
  91. int iterator = 0;
  92. int index = 0;
  93. int tmp = i;
  94.  
  95. while (tmp > p.n - 1) {
  96. if (p.next != null) {
  97. iterator += p.n;
  98. p = p.next;
  99. tmp = tmp - i;
  100. index = -1;
  101. } else {
  102. throw new IndexOutOfBoundsException();
  103. }
  104. }
  105.  
  106.  
  107. if (p.A[index] == null) {
  108. throw new IndexOutOfBoundsException("the desired index is too high");
  109. }
  110. return p.A[index];
  111. }
  112.  
  113. // if(i > p.n-1){
  114. // iterator += p.n;
  115. // p = p.next;
  116. // index = -1;
  117. // }
  118. // while (iterator != i) {
  119. //
  120. // if (iterator != 0 && iterator % (N - 1) == 0) {
  121. // if (p.next == null) {
  122. // throw new IndexOutOfBoundsException("the desired index is too high");
  123. // } else {
  124. // p = p.next;
  125. // index = -1;
  126. // }
  127. // }
  128. // iterator++;
  129. // index++;
  130. // }
  131. // if(p.A[index] == null){
  132. // throw new IndexOutOfBoundsException("the desired index is too high");
  133. // }
  134. // return p.A[index];
  135. // }
  136.  
  137. public boolean contains(T key) {
  138. ArrayListItem<T> p = head;
  139. int index = 0;
  140.  
  141. while (p.A[index] != key) {
  142. if (index + 1 == N) {
  143. if (p.next != null) {
  144. index = -1;
  145. p = p.next;
  146. } else {
  147. return false;
  148. }
  149. }
  150. index++;
  151. }
  152. return true;
  153. }
  154.  
  155. public static void main(String[] args) {
  156. ArrayList<Integer> list = new ArrayList<>(3);
  157. list.add(1);
  158. list.add(2);
  159. list.add(3);
  160. list.add(4);
  161. list.add(5);
  162. list.add(6);
  163. System.out.println(Arrays.toString(list.head.A));
  164. System.out.println(Arrays.toString(list.head.next.A));
  165. System.out.println(list.remove(1));
  166. System.out.println(list.remove(3));
  167. System.out.println(list.get(0));
  168. System.out.println(list.get(1));
  169. System.out.println(list.get(2));
  170. // System.out.println(list.get(3));
  171. }
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement