Advertisement
Tsuki11

Untitled

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