Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.48 KB | None | 0 0
  1. public class ArrayList{
  2.     public Integer[] list;
  3.     int numElement;
  4.    
  5.     public ArrayList() {
  6.         list = new Integer[] {0,0,1,1,7,0,1,3};
  7.         numElement = 8;
  8.     }
  9.    
  10.     public void remove(Integer target) {
  11.         numElement = removeAt(target, 0, 0);
  12.     }
  13.    
  14.     public int removeAt(Integer target, int cur_index, int array_index) {
  15.         if (cur_index == numElement) {
  16.             return 0;
  17.         }
  18.         if (list[cur_index].compareTo(target) != 0 && list [array_index].compareTo(target) != 0) {
  19.             return (1 + removeAt(target, cur_index + 1, array_index + 1));
  20.         } else {
  21.             while (list[cur_index].compareTo(target) == 0) {
  22.                 cur_index++;
  23.             }
  24.             list[array_index] = list[cur_index];
  25.             return (1 + removeAt(target, cur_index + 1, array_index + 1));
  26.         }
  27.     }
  28.    
  29.     public int removeAt2(Integer target, int cur_index, int array_index){
  30.         if(array_index>=numElement) return cur_index;
  31.         if(list[array_index].compareTo(target)==0)
  32.             return removeAt(target, cur_index, array_index+1);
  33.         list[cur_index] = list[array_index];
  34.         return removeAt(target, cur_index+1, array_index+1);
  35.     }
  36.    
  37.     public static void main(String[] args){
  38.         ArrayList a = new ArrayList();
  39.         a.remove(1);
  40.         for(Integer i : a.list){
  41.             System.out.println(i);
  42.         }
  43.         System.out.println("num elements " + a.numElement);
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement