Advertisement
Guest User

eieieieieie

a guest
Aug 26th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. /**
  2. * Collection
  3. */
  4. public interface Collection {
  5.  
  6. public void add(Object element);
  7. public void remove(Object element);
  8. public boolean isEmpty();
  9. public boolean contains(Object element);
  10. public int size();
  11. public void removeAll(Object e);
  12. public int frequency(Object e);
  13. public String toString();
  14. }
  15.  
  16. import java.util.Arrays;
  17.  
  18. /**
  19. * ArrayCollection
  20. */
  21. public class ArrayCollection implements Collection{
  22.  
  23. private Object[] elementData;
  24. private int size;
  25. public ArrayCollection(int c){
  26. elementData = new Object[c];
  27. size=0;
  28. }
  29. public ArrayCollection(){
  30. elementData = new Object[0];
  31. size=0;
  32. }
  33. public void add(Object e) {
  34. if(e == null) throw new IllegalArgumentException();
  35. ensureCapacity(size+1);
  36. elementData[size++] = e;
  37. }
  38. public int size(){
  39. return size;
  40. }
  41. public boolean isEmpty() {
  42. return size == 0;
  43. }
  44. public boolean contains(Object e) {
  45. return indexOf(e) != -1;
  46. }
  47. public void remove(Object e) {
  48. int i = indexOf(e);
  49. if (i != -1) {
  50. elementData[i] = elementData[--size];
  51. elementData[size] = null;
  52. }
  53. }
  54. private int indexOf(Object e) {
  55. for (int i=0; i<size; i++){
  56. if(elementData[i].equals(e)) return i;
  57. }
  58. return -1;
  59. }
  60. private void ensureCapacity(int capacity) {
  61. if (capacity > elementData.length) {
  62. int s = Math.max(capacity, 2*elementData.length);
  63. Object[] arr = new Object[s];
  64. for(int i = 0; i < size; i++)
  65. arr[i] = elementData[i];
  66. elementData = arr;
  67. }
  68. }
  69. public void removeAll(Object e) {
  70. for (int i = 0; i < size; i++) {
  71. if(elementData[i].equals(e)) remove(elementData[i]);
  72. }
  73. }
  74. public int frequency(Object e) {
  75. int counter=0;
  76. for (int i = 0; i < size; i++) {
  77. if(elementData[i].equals(e)) counter++;
  78. }
  79. return counter;
  80. }
  81. public String toString() {
  82. String result = "[";
  83. for (int i = 0; i < size; i++) {
  84. result+=elementData[i];
  85. if(i!=size-1){
  86. result+=",";
  87. }
  88. }
  89. result+="]";
  90. return result;
  91. }
  92. }
  93.  
  94. /**
  95. * main
  96. */
  97. public class main {
  98.  
  99. public static void main(String[] args) {
  100. Collection name = new ArrayCollection();
  101. name.add("AJ");
  102. name.add("CK");
  103. name.add("TJ");
  104. name.add("CK");
  105. name.add("MP");
  106. name.add("GG");
  107. name.add("AJ");
  108. name.add("SB");
  109. name.add("AJ");
  110. System.out.println(name.toString());
  111. System.out.println("num AJ: "+name.frequency("AJ"));
  112. name.removeAll("AJ");
  113. System.out.println(name.toString());
  114. }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement