Advertisement
Tsuki11

Untitled

Jun 15th, 2020
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. package E8_CustomListSort;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Iterator;
  5. import java.util.Objects;
  6.  
  7. public class CustomList<E extends Comparable<E>> implements Iterable<E> {
  8. private static final int INITIAL_CAPACITY = 4;
  9. private Object[] elements;
  10. private int size;
  11.  
  12. public CustomList() {
  13. this.elements = new Object[INITIAL_CAPACITY];
  14. }
  15.  
  16. public E getMin() {
  17. E min = this.getAt(0);
  18. for (int i = 0; i < this.size; i++) {
  19. if (this.getAt(i).compareTo(min) < 0) {
  20. min = this.getAt(i);
  21. }
  22. }
  23. return min;
  24. }
  25.  
  26. public E getMax() {
  27. E max = this.getAt(0);
  28. for (int i = 0; i < this.size; i++) {
  29. if (this.getAt(i).compareTo(max) > 0) {
  30. max = this.getAt(i);
  31. }
  32. }
  33. return max;
  34. }
  35.  
  36. public int getCountGreater(E element) {
  37. int counter = 0;
  38. for (int i = 0; i < this.size; i++) {
  39. if (this.getAt(i).compareTo(element) > 0) {
  40. counter++;
  41. }
  42. }
  43. return counter;
  44. }
  45.  
  46. public void swapIndexes(int i1, int i2) {
  47. checkIndex(i1);
  48. checkIndex(i2);
  49. E temp = this.getElement(i1);
  50. this.elements[i1] = this.elements[i2];
  51. this.elements[i2] = temp;
  52. }
  53.  
  54. public boolean containsElement(E element) {
  55. return Arrays.stream(this.elements).filter(Objects::nonNull).anyMatch(a -> a.equals(element));
  56. }
  57.  
  58. public void removeElement(int index) {
  59. checkIndex(index);
  60. if (this.size - 1 - index >= 0) {
  61. System.arraycopy(this.elements, index + 1, this.elements, index, this.size - 1 - index);
  62. this.elements[this.size - 1] = null;
  63. this.size--;
  64. }
  65. }
  66.  
  67. public void addElement(E element) {
  68. if (this.size == this.elements.length) {
  69. this.elements = resizeMyList();
  70. }
  71. this.elements[this.size++] = element;
  72. }
  73.  
  74. public E getElement(int index) {
  75. checkIndex(index);
  76. return this.getAt(index);
  77. }
  78.  
  79. @SuppressWarnings("unchecked")
  80. private E getAt(int index) {
  81. return (E) this.elements[index];
  82. }
  83.  
  84. private void checkIndex(int index) {
  85. if (index < 0 || index >= this.size) {
  86. throw new IndexOutOfBoundsException(String.format("Index: %d, Size: %d", index, size));
  87. }
  88. }
  89.  
  90. private Object[] resizeMyList() {
  91. return Arrays.copyOf(this.elements, this.elements.length * 2);
  92. }
  93.  
  94. public void printList() {
  95. Arrays.stream(this.elements).filter(Objects::nonNull).forEach(System.out::println);
  96. }
  97.  
  98.  
  99. public int sizeObj() {
  100. return this.size;
  101. }
  102.  
  103. public void sort() {
  104. Sorter<E> sorter = new Sorter<>();
  105. Sorter.sort(this);
  106. }
  107.  
  108. @Override
  109. public Iterator<E> iterator() {
  110. return new Iterator<E>() {
  111. private int index = 0;
  112.  
  113. @Override
  114. public boolean hasNext() {
  115. return index < size;
  116. }
  117.  
  118. @Override
  119. public E next() {
  120. return getElement(index++);
  121. }
  122. };
  123. }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement