Adam DiStefano
Adam DiStefano

Reputation: 11

List Interface - Java

I am working with the code below. The list interface specifies two overloaded remove() methods. I cannot figure out how to determine which one Java uses if we invoke remove(3) on a List. How can we force Java to use the other one?

public class ArrayList<E> implements List1<E> {

    private E[] data;
    private int size;

    public ArrayList(){
        data = (E[]) (new Object[1]);
        size = 0;
    }

    public void add(E target) {
        if (isFull()) {
            stretch();
        }
        data[size] = target;
        size++;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    protected boolean isFull() {
        return size == data.length;
    }

    public E get(int index) {
        return data[index];
    }

    public void set(int index, E target) {
        data[index] = target;
    }

    public int size() {
        return size;
    }

    protected void stretch() {
        E[] newData = (E[]) (new Object[data.length * 2]);
        for (int i = 0; i < data.length; i++) {
            newData[i] = data[i];
        }
        data = newData;
    }


    public boolean contains(E target) {
        for (int i = 0; i < size; i++) {
            if (data[i].equals(target)) {
                return true;
            }
        }
        return false;
    }

    public String toString() {
        String result = "[";
        for (int i = 0; i < size; i++) {
            result += data[i] + "";
        }
        return result + "]";
    }

    public E remove(int index) {
        E result = data[index];
        for (int i = index; i < size; i++) {
            data[i - 1] = data[i];
        }
        size--;
        return result;
    }

    public boolean remove(E target) {
        for (int i = 0; i < size; i++) {
            if (data[i].equals(target)){
            }
            size--;
            return true;
        }
        return false;
    }

    public static interface List1<E> {

        public void add(E target);
        public boolean contains(E traget);
        public E get(int index);
        public boolean isEmpty();
        public E remove(int index);
        public boolean remove(E index);
        public void set(int index, E target);
        public int size();
    }
}

Upvotes: 1

Views: 101

Answers (2)

burglarhobbit
burglarhobbit

Reputation: 2291

You have two remove() functions with different return data types and different function overloading data types. So Java would be able to distinguish those functions based on these parameters and thus will choose the appropriate function of your call. You cannot just force Java to use the other one, unless you want to call it explicitly from the first one as follows:

remove(datatype1 var1) {
    remove(var2); //datatype2 of var2
    //your code
}

remove(datatype2 var) {
    //your code
}

Upvotes: 1

Gaurav Jeswani
Gaurav Jeswani

Reputation: 4582

If you see these two remove methods it's very clear that one take index of object in list while other take Object to delete. And the object is the Object of the Type of list. So if you want to use other one simply pass the object which is being contained by arrayList. For example:

If your list contains Integers:

List<Foo> integerList = new ArrayList<Foo>();
Foo foo = new Foo();
Foo foo1 = new Foo();
integerList.add(foo);
integerList.add(foo1);
integerList.remove(foo);//remove 1
integerList.remove(0);//remove 2

In above remove1 call the method remove(E target) will get called, on the other hand at remove 2 call the method remove(int index) will get called.

Upvotes: 0

Related Questions