Advertisement
Kulas_Code20

Array

Sep 24th, 2021
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.45 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 Arraylist {
  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 Arraylist(int size) {
  28.         // preparing the number of elements the array can hold
  29.         element = new int[size];
  30.     }
  31.  
  32.     public Arraylist() {
  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.     /**
  62.      * count must be less than the size of the array, otherwise, you will encounter
  63.      * ArrayIndexOutOfBoundsException this will cause ArrayIndexOutBoundException if
  64.      * array is full,but wont because of the if statement
  65.      */
  66.     public void addLast(int number) {
  67.         if (isFull())
  68.             doubleTheArray();
  69.         element[count++] = number;     
  70.     }
  71.  
  72.     private void shiftLeft(int loc) {
  73.  
  74.     }
  75.  
  76.     private void shiftRight(int loc) {
  77.         int pos = count;
  78.         while (loc < pos)
  79.             element[pos] = element[--pos];
  80.     }
  81.  
  82.     public void addFirst(int number) {
  83.         if (isFull())  
  84.             doubleTheArray();
  85.         if (!isEmpty())
  86.             shiftRight(0);
  87.             element[count++] = number;
  88.        
  89.     }
  90.  
  91.     public void insertItemAt(int number, int position) {
  92.  
  93.     }
  94.  
  95.     public int[] removeFirst() {
  96.         int[] newElem = null;
  97.         if (!isEmpty())
  98.             newElem = new int[element.length-1];
  99.             for (int i = 0; i < newElem.length; i++)
  100.                 newElem[i] = element[i+1];
  101.            
  102.         return newElem;
  103.     }
  104.  
  105.     public int[] removeLast() {
  106.         int[] newElem = null;
  107.         if (!isEmpty())
  108.             newElem = new int[element.length-1];
  109.             for (int i = 0; i < newElem.length; i++)
  110.                 newElem[i] = element[i];
  111.            
  112.         return newElem;
  113.     }
  114.  
  115.     public void removeItemAt(int position) {
  116.  
  117.     }
  118.  
  119.     public boolean isFound(int number) {
  120.         return false;
  121.  
  122.     }
  123.  
  124.     public String toString() {
  125.         // we override toString so that the object of ArrayList will have a String
  126.         // representation, which is to display the elements from the beginning to the
  127.         // end
  128.         StringBuffer sb = new StringBuffer();
  129.         sb.append("{ ");
  130.         for (int i = 0; i < count; i++)
  131.             sb.append(element[i] + " ");
  132.         sb.append("}");
  133.         return sb.toString();
  134.     }
  135.  
  136.     public int getPosition(int number) {
  137.         return number;
  138.  
  139.     }
  140.  
  141.     public void doubleTheValue() {
  142.         for (int i = 0; i < count; i++)
  143.             element[i] = element[i] * 2;
  144.     }
  145.  
  146.     public static void main(String[] args) {
  147.         Arraylist list = new Arraylist();
  148.         list.addLast(0);
  149.         list.addLast(2);
  150.         list.addLast(3);
  151.         list.addLast(4);
  152.         list.addLast(5);
  153.         list.addLast(6);
  154.         list.addLast(7);
  155.         list.addLast(8);
  156.        
  157.         System.out.println("List contains:" + list);
  158.         //list.addFirst(7);
  159.         //System.out.println("Add first element:" + list);
  160.         //list.removeFirst();
  161.         //System.out.println("List contains:" + list);
  162.        
  163.         //list.addFirst(7);
  164.         //list.addFirst(4);
  165.         //list.addFirst(12);
  166.         //System.out.println("List contains:" + list);
  167.         // Expected output: 9 4 7 5 7 8 9
  168.         /// System.out.println("List contains: " + list);
  169.         // list.insertItemAt(10, 3);
  170.         // System.out.println("List contains:" + list);
  171.         //list.removeFirst();
  172.         //System.out.println("List contains:" + list);
  173.         list.removeLast();
  174.         System.out.println("RemoveLast List contains: " + list);
  175.         // list.removeItemAt(5);
  176.         //// System.out.println("List contains: " + list);
  177.         // list.doubleTheValue();
  178.         // System.out.println("List contains: " + list);
  179.         // System.out.println("is 5 found? " + list.isFound(5));
  180.  
  181.     }
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement