Advertisement
mr1302

lab7

Oct 29th, 2021
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Collection;
  5.  
  6. public class MyArrayList<E> {
  7. private Object[] elements;
  8. private int size = 0;
  9. private int capacity = 0;
  10.  
  11. public MyArrayList() {
  12. elements = new Object[10];
  13. size = 10;
  14. capacity = 0;
  15. }
  16.  
  17. public MyArrayList(int size) {
  18. this.size = size;
  19. elements = new Object[size];
  20. capacity = 0;
  21. }
  22.  
  23. public MyArrayList(Collection<? extends E> col) {
  24. Object[] a = col.toArray();
  25. size = col.size();
  26. capacity = size;
  27. elements = new Object[size];
  28. System.arraycopy(a, 0, elements, 0, size);
  29. }
  30.  
  31. public int size(){
  32. return capacity;
  33. }
  34.  
  35. void add(E e){
  36. if(capacity + 1 > size) {
  37. Object[] tempElements = new Object[size * 2];
  38. System.arraycopy(elements, 0, tempElements, 0, capacity);
  39. elements = tempElements;
  40. }
  41. elements[capacity++] = e;
  42. }
  43.  
  44. boolean addAll(Collection<? extends E> col){
  45. if(col.size() == 0)
  46. return false;
  47. if(capacity + col.size() > size){
  48. Object[] tempElements = new Object[size + col.size()];
  49. System.arraycopy(elements, 0, tempElements, 0, capacity);
  50. elements = tempElements;
  51. }
  52. Object[] a = col.toArray();
  53. System.arraycopy(a, 0, elements, capacity, col.size());
  54. capacity += col.size();
  55. return true;
  56. }
  57.  
  58. public E get(int index){
  59. return (E) elements[index];
  60. }
  61.  
  62. public E set(int index, E e){
  63. elements[index] = e;
  64. return e;
  65. }
  66.  
  67. public E remove(int index){
  68. E e = get(index);
  69. System.arraycopy(elements, index + 1, elements, index, size - index - 1);
  70. capacity--;
  71. return e;
  72. }
  73.  
  74. public boolean contains(E e){
  75. for(int i = 0; i < capacity; ++i){
  76. if(e.equals(elements[i]))
  77. return true;
  78. }
  79. return false;
  80. }
  81.  
  82. @Override
  83. public String toString() {
  84. String s = "";
  85. for(int i = 0; i < capacity; ++i){
  86. s += elements[i].toString() + ", ";
  87. }
  88. return s;
  89. }
  90. }
  91.  
  92.  
  93. package com.company;
  94.  
  95. import java.util.Arrays;
  96. import java.util.LinkedList;
  97.  
  98. public class Main {
  99.  
  100. public static void main(String[] args) {
  101. MyArrayList<Integer> a = new MyArrayList<>();
  102. a.add(1);
  103. a.add(2);
  104. a.add(3);
  105. a.add(4);
  106. a.add(5);
  107. a.add(6);
  108. System.out.println(a);
  109. a.remove(3);
  110. System.out.println(a);
  111. System.out.println(a.contains(6));
  112. a.set(0, 101);
  113. System.out.println(a.get(0));
  114. a.addAll(Arrays.asList(new Integer[]{1337, 501, 4654, 123}));
  115. System.out.println(a);
  116. }
  117. }
  118.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement