Advertisement
vdjalov

Untitled

Oct 4th, 2018
589
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. package customList;
  2.  
  3. import java.lang.reflect.Array;
  4. import java.util.ArrayList;
  5. import java.util.Arrays;
  6. import java.util.Iterator;
  7. import java.util.List;
  8.  
  9. @SuppressWarnings("rawtypes")
  10. public class CustomList <T extends Comparable<T>> implements Iterable {
  11.  
  12. private T array[];
  13. private int count;
  14.  
  15. @SuppressWarnings("unchecked")
  16. public CustomList(Class<T> clazz){
  17. this.array = (T[]) Array.newInstance(clazz, 5);
  18. this.count = 0;
  19. }
  20.  
  21. // Adding an element to the array!
  22. public void add(T element){
  23.  
  24. if(count <= this.array.length - 1){
  25. this.array[this.count] = element;
  26. this.count++;
  27. }else{
  28. this.doubleArraySize();
  29. this.array[this.count] = element;
  30. this.count++;
  31. }
  32. }
  33.  
  34. // Removing an element from the list
  35. public T remove(int index){
  36. if(index < 0 || index > this.count || array[index] == null){
  37. throw new IndexOutOfBoundsException();
  38. }
  39. T element = this.array[index];
  40.  
  41. for(int i = index; i < this.count; i++){
  42. if(i + 1 == this.count || array[i + 1] == null){
  43. array[i] = null;
  44. this.count--;
  45. break;
  46. }
  47. array[i] = array[i + 1];
  48. }
  49. return element;
  50. }
  51.  
  52. // Checking if the array contains
  53. public boolean contains(T element){
  54. boolean isTrue = false;
  55. for(int i = 0; i < this.count; i++){
  56. if(this.array[i].compareTo(element) == 0){
  57. isTrue = true;
  58. break;
  59. }
  60. }
  61. return isTrue;
  62. }
  63.  
  64. // Swapping two elements
  65. public void swap(int index1, int index2){
  66. if(index1 < 0 || index2 < 0 || index1 >= count || index2 >= count){
  67. throw new IndexOutOfBoundsException();
  68. }
  69.  
  70. T element1 = this.array[index1];
  71. T element2 = this.array[index2];
  72. array[index1] = element2;
  73. array[index2] = element1;
  74.  
  75. }
  76.  
  77. // Count greater than T elements
  78. public int countGreaterThan(T element){
  79. int count = 0;
  80. for(int i = 0; i < this.count; i++){
  81. if(array[i].compareTo(element) > 0){
  82. count++;
  83. }
  84. }
  85. return count;
  86. }
  87.  
  88. // Returns max element
  89. public T getMax(){
  90. T maxElement = this.array[0];
  91. for(int i = 1; i < this.count; i++){
  92. if(maxElement.compareTo(array[i]) < 0){
  93. maxElement = array[i];
  94. }
  95. }
  96. return maxElement;
  97. }
  98.  
  99. // Returns min element
  100. public T getMin(){
  101. T minElement = this.array[0];
  102. for(int i = 1; i < this.count; i++){
  103. if(minElement.compareTo(array[i]) > 0){
  104. minElement = array[i];
  105. }
  106. }
  107. return minElement;
  108. }
  109.  
  110. // Printing the elements of the CustomList
  111. @SuppressWarnings({ "unchecked" })
  112. public void print(){
  113. // StringBuilder sb = new StringBuilder();
  114. //for(int i = 0; i < this.count; i++){
  115. // sb.append(this.array[i]).append(System.lineSeparator());
  116. //}
  117. //System.out.print(sb.toString());
  118.  
  119. Iterator it = this.iterator(); // Printing the List by implementing iterator.
  120. it.forEachRemaining(a ->{ // Iterator does not work with an array!
  121. if(a != null){
  122. System.out.println(a);
  123. }
  124. });
  125. }
  126.  
  127.  
  128.  
  129.  
  130. private void doubleArraySize(){ // Doubles the list size!
  131. @SuppressWarnings("unchecked")
  132. T arrayCopy[] = (T[]) Array.newInstance(this.array[0].getClass(), this.count * 2);
  133. System.arraycopy(array, 0, arrayCopy, 0, this.count);
  134. this.array = arrayCopy;
  135. }
  136.  
  137. // Returns list length
  138. public int getLength(){
  139. return this.count;
  140. }
  141.  
  142. // Return index
  143. public T get(int index){
  144. return this.array[index];
  145. }
  146.  
  147. public void set(int index, T element){
  148. this.array[index] = element;
  149. }
  150.  
  151. // Iterator
  152. @Override
  153. public Iterator iterator() {
  154. List<T> list = new ArrayList<T>();
  155. list = Arrays.asList(this.array);
  156. Iterator<T> it = list.iterator();
  157. return it;
  158.  
  159. }
  160.  
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement