Advertisement
Guest User

Untitled

a guest
Mar 20th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | None | 0 0
  1. public class OwnArrayList {
  2.  
  3. Object [] tab;
  4. int currentSize;
  5. int numberOfElements=0;
  6.  
  7. public OwnArrayList(int startSize){
  8. tab = new Object[startSize];
  9. currentSize=startSize;
  10. }
  11. public OwnArrayList(){
  12.  
  13. tab = new Object[10];
  14. currentSize=10;
  15. }
  16. public Object get(int index){
  17.  
  18. if(index<currentSize && index>-1){
  19. return tab[index];
  20. }
  21. else {System.out.println("Incorrect index");
  22. return null;
  23. }
  24. }
  25. public Object set(int index, Object o) {
  26.  
  27. if (index < currentSize && index > -1) {
  28. Object actual = tab[index];
  29. tab[index] = o;
  30. return actual;
  31. }
  32. else {System.out.println("Incorrect index");
  33. return null;
  34. }
  35. }
  36. private Object[] increaseCapacity() {
  37.  
  38. Object [] newTab;
  39. double temp=0;
  40.  
  41. if(currentSize<=5){
  42. temp =10;
  43. }
  44. else if(currentSize>5){
  45. temp = 1.6*currentSize;}
  46. newTab = new Object[(int)temp];
  47. currentSize=newTab.length;
  48. for (int i = 0; i <tab.length; i++) {
  49. newTab[i] = tab[i];
  50. }
  51. tab=newTab;
  52. return tab;
  53. }
  54. public int size(){return numberOfElements;}
  55.  
  56. public boolean add(Object o) {
  57.  
  58. int currentLength = currentSize;
  59.  
  60. if (isFull()) {
  61. increaseCapacity();
  62. tab[currentLength] = o;
  63. numberOfElements++;
  64. return true;
  65. }
  66. else if (!isFull() && tab[0] != null) {
  67. for (int i = currentSize - 1; i >= 0; i--) {
  68. if (tab[i] != null) {
  69. tab[i + 1] = o;
  70. numberOfElements++;
  71. return true;
  72. }
  73. }
  74. }
  75. else if (!isFull() && tab[0] == null) {
  76. tab[0] = o;
  77. numberOfElements++;
  78. return true;
  79. }
  80. return true;
  81. }
  82. public int find(Object o){
  83.  
  84. for(int i=0;i<currentSize;i++){
  85. if(tab[i].equals(o)){
  86. return i;}
  87. }
  88. return -1;
  89.  
  90. }
  91. private boolean isFull(){
  92. return numberOfElements==currentSize;
  93. }
  94.  
  95. public boolean remove(Object o) {
  96.  
  97. if (find(o) == -1) {
  98. return false;
  99. } else {
  100. Object[] newTab = new Object[currentSize - 1];
  101. int i = 0;
  102. for (int j = 0; j < currentSize; j++) {
  103. if (j < i) {
  104. newTab[j] = tab[j];
  105. } else if (j > i) {
  106. newTab[j - 1] = tab[j];
  107. }
  108. }
  109. tab = newTab;
  110. currentSize--;
  111. numberOfElements--;
  112. return true;
  113. }
  114. }
  115. public void printer(){
  116.  
  117. for(int i=0;i<currentSize;i++)
  118. System.out.println(tab[i]);
  119. }
  120. public static void main(String []args){
  121.  
  122. OwnArrayList list = new OwnArrayList(3);
  123. System.out.println(list.size());
  124. list.add("one");
  125. list.add("two");
  126. list.add("three");
  127. list.add("four");
  128. list.add("five");
  129. list.add("six");
  130. list.add("seven");
  131. list.add("eight");
  132. list.add("nine");
  133. list.add("ten");
  134. list.add("eleven");
  135. System.out.println(list.isFull());
  136. System.out.println(list.numberOfElements);
  137. list.printer();
  138. list.remove("one");
  139. list.set(3,"$$$");
  140. list.printer();
  141. list.find("two");
  142. System.out.println(list.size());
  143. }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement