Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ArrayList{
- public Integer[] list;
- int numElement;
- public ArrayList() {
- list = new Integer[] {0,0,1,1,7,0,1,3};
- numElement = 8;
- }
- public void remove(Integer target) {
- numElement = removeAt(target, 0, 0);
- }
- public int removeAt(Integer target, int cur_index, int array_index) {
- if (cur_index == numElement) {
- return 0;
- }
- if (list[cur_index].compareTo(target) != 0 && list [array_index].compareTo(target) != 0) {
- return (1 + removeAt(target, cur_index + 1, array_index + 1));
- } else {
- while (list[cur_index].compareTo(target) == 0) {
- cur_index++;
- }
- list[array_index] = list[cur_index];
- return (1 + removeAt(target, cur_index + 1, array_index + 1));
- }
- }
- public int removeAt2(Integer target, int cur_index, int array_index){
- if(array_index>=numElement) return cur_index;
- if(list[array_index].compareTo(target)==0)
- return removeAt(target, cur_index, array_index+1);
- list[cur_index] = list[array_index];
- return removeAt(target, cur_index+1, array_index+1);
- }
- public static void main(String[] args){
- ArrayList a = new ArrayList();
- a.remove(1);
- for(Integer i : a.list){
- System.out.println(i);
- }
- System.out.println("num elements " + a.numElement);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement