Advertisement
Guest User

Untitled

a guest
Feb 20th, 2020
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.95 KB | None | 0 0
  1. package a1;
  2.  
  3. import java.util.Scanner;
  4.  
  5. import CustomArrayList.StaticArrayList_Main;
  6.  
  7. public class CustomArrayList {
  8.  
  9.         private Object[] arr;
  10.  
  11.         private int count;
  12.  
  13.         private static final int INITIAL_CAPACITY = 4;
  14.  
  15.         public CustomArrayList() {
  16.  
  17.             arr = new Object[INITIAL_CAPACITY];
  18.  
  19.             count = 0;
  20.  
  21.         }
  22.  
  23.    
  24.  
  25.         public int getLength() {
  26.  
  27.             return count;
  28.  
  29.         }
  30.  
  31. public void add(Object item) {
  32.  
  33.     add(count, item);
  34.  
  35. }
  36.  
  37. public void add(int index, Object item) {
  38.  
  39.     if (index>count || index<0) {
  40.  
  41.         throw new IndexOutOfBoundsException(
  42.  
  43.             "Invalid index: " + index);
  44.  
  45.     }
  46.  
  47.     Object[] extendedArr = arr;
  48.  
  49.     if (count + 1 == arr.length) {
  50.  
  51.         extendedArr = new Object[arr.length * 2];
  52.  
  53.     }
  54.  
  55.  
  56.  
  57.     System.arraycopy(arr, 0, extendedArr, 0, index);      
  58.  
  59.     count++;
  60.  
  61.     System.arraycopy(
  62.  
  63.         arr, index, extendedArr, index+1, count-index-1);
  64.  
  65.     extendedArr[index] = item;
  66.  
  67.     arr = extendedArr;
  68.  
  69. }
  70.  
  71. public int indexOf(Object item) {
  72.  
  73.     if (item == null) {
  74.  
  75.         for (int i = 0; i < arr.length; i++) {
  76.  
  77.             if (arr[i] == null)
  78.  
  79.                 return i;
  80.  
  81.         }
  82.  
  83.     } else {
  84.  
  85.         for (int i = 0; i < arr.length; i++)
  86.  
  87.             if (item.equals(arr[i]))
  88.  
  89.                 return i;
  90.  
  91.     }
  92.  
  93.     return -1;
  94.  
  95. }
  96.  
  97. public void clear() {
  98.  
  99.     arr = new Object[0];
  100.  
  101.     count = 0;
  102.  
  103. }
  104.  
  105. public boolean contains(Object item) {
  106.  
  107.     int index = indexOf(item);
  108.  
  109.     boolean found = (index != -1);
  110.  
  111.     return found;
  112.  
  113. }
  114.  
  115.  
  116. public Object elementAt(int index) {
  117.  
  118.     if (index>=count || index<0) {
  119.  
  120.         throw new IndexOutOfBoundsException(
  121.  
  122.             "Invalid index: " + index);
  123.  
  124.     }
  125.  
  126.     return arr[index];
  127.  
  128. }
  129.  
  130. public Object remove(int index) {
  131.  
  132.     if (index>=count || index<0) {
  133.  
  134.         throw new IndexOutOfBoundsException(
  135.  
  136.             "Invalid index: " + index);
  137.  
  138.     }
  139.  
  140.     Object item = arr[index];
  141.  
  142.     System.arraycopy(arr, index+1, arr, index, count-index+1);
  143.  
  144.     arr[count - 1] = null;
  145.  
  146.     count--;
  147.  
  148.     return item;
  149.  
  150. }
  151.  
  152. public int remove(Object item) {
  153.  
  154.     int index = indexOf(item);
  155.  
  156.     if (index == -1) {
  157.  
  158.         return index;
  159.  
  160.     }
  161.  
  162.     System.arraycopy(arr, index+1, arr, index, count-index+1);
  163.  
  164.     count--;
  165.  
  166.     return index;
  167.  
  168. }
  169.  
  170. public static void main(String[] args){
  171.  
  172.     CustomArrayList shoppingList = new CustomArrayList();
  173.  
  174.     shoppingList.add("Milk");
  175.  
  176.     shoppingList.add("Honey");
  177.  
  178.     shoppingList.add("Olives");
  179.  
  180.     shoppingList.add("Beer");
  181.  
  182.     shoppingList.remove("Olives");
  183.  
  184.     System.out.println("We need to buy:");
  185.  
  186.     for(int i=0; i<shoppingList.getLength(); i++) {
  187.  
  188.         System.out.println(shoppingList.elementAt(i));
  189.  
  190.     }
  191.  
  192.     System.out.println("Do we have to buy Bread? " +
  193.  
  194.         shoppingList.contains("Bread"));
  195.    
  196.     CustomArrayList shoppingList1 = new CustomArrayList();
  197.     Scanner sc=new Scanner(System.in);
  198.  
  199.     while(true)
  200.     {
  201.         System.out.println();
  202.         System.out.println("For ADD choose 1, for REMOVE choose 2, for END choose 3");
  203.             int choice = sc.nextInt();
  204.        
  205.         if(choice==1)
  206.         {
  207.             System.out.println("The element you want to add:");
  208.             String n=sc.next();
  209.            
  210.             if(shoppingList1.contains(n)==false)
  211.             {
  212.                 shoppingList1.add(n);
  213.                
  214.                 System.out.println();
  215.                 System.out.println("Now your list contains:");
  216.                 getList(shoppingList1);
  217.                
  218.                 continue;
  219.                
  220.             } else
  221.             {
  222.                 System.out.println("The element is already in the list");
  223.                 continue;
  224.             }
  225.         }
  226.        
  227.         if(choice==2)
  228.         {
  229.             System.out.println("The element you want to remove:");
  230.             String n=sc.next();
  231.            
  232.             if(shoppingList1.contains(n))
  233.             {
  234.                 shoppingList1.remove(shoppingList1.indexOf(n));
  235.                
  236.                 System.out.println();
  237.                 System.out.println("Now your list contains:");
  238.                 getList(shoppingList1);
  239.                
  240.                 continue;
  241.                
  242.             } else
  243.             {
  244.                 System.out.println("No element to remove");
  245.                 continue;
  246.             }
  247.         }
  248.        
  249.         if(choice==3)
  250.         {
  251.             System.out.println();
  252.             System.out.println("Now your list contains:");
  253.             getList(shoppingList1);
  254.            
  255.             System.out.println("Program ended");
  256.             break;
  257.         }
  258.        
  259.        
  260.     } // end while
  261.    
  262.     sc.close();
  263.    
  264. } // end MAIN
  265.  
  266.  
  267.  
  268. static void getList(CustomArrayList shoppingList)
  269. {
  270.     for(int i=0; i<shoppingList.getLength(); i++)
  271.     {
  272.         System.out.println(shoppingList.elementAt(i));
  273.     }
  274. }
  275.  
  276.  
  277. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement