Advertisement
Kulas_Code20

MyArraylist

Sep 25th, 2021
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.31 KB | None | 0 0
  1. package otherfiles.com;
  2.  
  3. /*
  4. This ArrayList class contains two instance variables:
  5. int element[], and count;
  6. The following methods are part of the class:
  7.  public void addFirst(int number);
  8.  public void addLast(int number);
  9.  public void insertItemAt(int number,int position);
  10.  public void removeFirst();
  11.  public void removeLast();
  12.  public void removeItemAt(int position);
  13.  public boolean isFound(int number);
  14.  public int getPosition(int number);
  15.  public String toString()
  16.  
  17.  Some methods are already completed. Complete the other methods
  18.  and test each method to see if they are working correctly.
  19.  
  20. */
  21. public class MyArraylist {
  22.  
  23.     int[] element;// this is your single dimensional array
  24.     protected int count; // count will be used for monitoring the exact number of elements in element
  25.                             // array
  26.  
  27.     public MyArraylist(int size) {
  28.         // preparing the number of elements the array can hold
  29.         element = new int[size];
  30.     }
  31.  
  32.     public MyArraylist() {
  33.         // the array is set to hold 10 elements
  34.         // if this constructor is called, the array can hold 10 elements
  35.         this(10);
  36.     }
  37.  
  38.     public int getFirstElement() {
  39.         return (!isEmpty()) ? element[0] : -1;
  40.     }
  41.  
  42.     public int getLastElement() {
  43.         return (!isEmpty()) ? element[count - 1] : -1; // ternary operator
  44.     }
  45.  
  46.     public void doubleTheArray() {
  47.         int[] temp = new int[element.length * 2];
  48.         for (int i = 0; i < element.length; i++)
  49.             temp[i] = element[i];
  50.         element = temp;
  51.     }
  52.  
  53.     public boolean isEmpty() {
  54.         return count == 0;
  55.     }
  56.  
  57.     public boolean isFull() {
  58.         return count == element.length;
  59.     }
  60.     /**
  61.      * count must be less than the size of the array, otherwise, you will encounter
  62.      * ArrayIndexOutOfBoundsException this will cause ArrayIndexOutBoundException if
  63.      * array is full,but wont because of the if statement
  64.      */
  65.     public void addLast(int number) {
  66.         if (isFull())
  67.             doubleTheArray();
  68.         element[count++] = number;     
  69.     }
  70.     //arr = 9 1 11 12 4 3 4 5 6 7
  71.     //output = 12 4 3 4 5 6 7 9 1 11
  72.     private void shiftLeft(int location) {
  73.         int j;
  74.         int temp = element[0];
  75.         for(int i=0; i<location; i++) {
  76.             //int temp = element[0];
  77.             for(j=0; j<element.length-1; j++) {
  78.                 element[j] = element[j+1];
  79.             }
  80.             element[j] = temp;
  81.         }
  82.     }
  83.  
  84.     private void shiftRight(int location,int number) {
  85.         count++;
  86.         for(int i=element.length-1; i>location; i--) {
  87.             element[i] = element[i-1];
  88.         }
  89.         element[location] = number;
  90.     }
  91.  
  92.     public void addFirst(int number) {
  93.         if (isFull())  
  94.             doubleTheArray();
  95.         if (!isEmpty()) {
  96.             shiftRight(0,number);
  97.         }
  98.     }
  99.    
  100.     public void removeFirst() {
  101.         int[] newElem = null;
  102.         if (!isEmpty()) {
  103.             newElem = new int[element.length-1];
  104.             for (int i = 0; i < newElem.length; i++) {
  105.                 newElem[i] = element[i+1];
  106.             }count--;
  107.         }  
  108.         element = newElem;
  109.     }
  110.  
  111.     public void removeLast() {
  112.         int[] newElem = null;
  113.         if (!isEmpty()) {
  114.             newElem = new int[element.length-1];
  115.             for (int i = 0; i < newElem.length; i++)
  116.                 newElem[i] = element[i];
  117.             count--;
  118.         }  
  119.         element = newElem;
  120.     }
  121.  
  122.     public void insertItemAt(int number, int position) {
  123.         count++;
  124.         for(int i=element.length-1; i>position; i--) {
  125.             element[i] = element[i-1];
  126.         }
  127.         element[position] = number;
  128.     }
  129.  
  130.     public void removeItemAt(int position) {
  131.         count--;
  132.         for(int i=position; i<element.length-1; i++) {
  133.             element[i] = element[i+1];
  134.         }
  135.     }
  136.  
  137.     public boolean isFound(int number) {
  138.         boolean res = false;
  139.         for(int i=0; i<element.length; i++) {
  140.             if(element[i]==number) {
  141.                 return true;
  142.             }
  143.         }
  144.         return res;
  145.     }
  146.  
  147.     public String toString() {
  148.         // we override toString so that the object of ArrayList will have a String
  149.         // representation, which is to display the elements from the beginning to the
  150.         // end
  151.         StringBuffer sb = new StringBuffer();
  152.         sb.append("[ ");
  153.         for (int i = 0; i < count; i++)
  154.             sb.append(element[i] + " ");
  155.         sb.append("]");
  156.         return sb.toString();
  157.     }
  158.  
  159.     public int getPosition(int number) {
  160.         int count=0;
  161.         for(int i=0; i<element.length; i++) {
  162.             if(element[i]==number) {
  163.                 break;
  164.             }
  165.             count++;
  166.         }
  167.         return count;
  168.     }
  169.  
  170.     public void doubleTheValue() {
  171.         for (int i = 0; i < count; i++)
  172.             element[i] = element[i] * 2;
  173.     }
  174.  
  175.     public static void main(String[] args) {
  176.         MyArraylist list = new MyArraylist();
  177.         //list.addLast(0);
  178.         list.addLast(2);
  179.         list.addLast(3);
  180.         list.addLast(4);
  181.         list.addLast(5);
  182.         list.addLast(6);
  183.         list.addLast(7);
  184.         list.addLast(8);
  185.         System.out.println("List contains:" + list);
  186.         list.removeFirst();
  187.         System.out.println("List contains:" + list);
  188.         list.addFirst(7);
  189.         list.addFirst(4);
  190.         list.addFirst(12);
  191.         list.addFirst(1);
  192.         list.addFirst(9);
  193.         list.addFirst(10);
  194.         System.out.println("Add first element:" + list);
  195.         list.insertItemAt(11, 3);
  196.         System.out.println("InsertItem List contains: " + list);
  197.         list.removeFirst();
  198.         System.out.println("RemoveFirst List contains:" + list);
  199.         list.removeLast();
  200.         System.out.println("RemoveLast List contains: " + list);
  201.         list.removeItemAt(5);
  202.         System.out.println("removeItem List contains: " + list);
  203.         list.shiftLeft(3);
  204.         System.out.println("ShiftLeft List contains: " + list);
  205.         System.out.println("Location of 12 is at index: " + list.getPosition(12));
  206.         System.out.println("is 5 found? " + list.isFound(5));
  207.         list.doubleTheValue();
  208.         System.out.println("List contains: " + list);
  209.  
  210.     }
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement