Kulas_Code20

Arraylist

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