user4833678
user4833678

Reputation:

Initializing generic constructor

When I do the following:

 IMiniMap<String,Integer> map = new SimpleListMM<String,Integer>();
 IMiniMap<Double,ArrayList<Object>> map2 = new SimpleListMM<Double,ArrayList<Object>>();
 IMiniMap<String,Integer> map = new SimpleListMM<String,Integer>();

then I get an error saying that The constructor SimpleListMM<...,...>() is undefined. I'm not allowed to have setter methods and all I did in constructor is to assign ArrayList<K> smth and ArrayList<V>. Whats the approach for intializing generic constructors in classes? How should I fix that?

import java.util.*;

public class FastGetListMM<K,V> extends AbstractListMM<K,V> implements Comparator<K> {

    // Comparator used to sort elements; may be null if elements are Comparable
    public final Comparator<K> cmp = new Comparator<K>();    
    //private List<K> keys;;
    //private List<V> values;

    // Assume elements must be comparable
    public FastGetListMM(ArrayList<K> keys, ArrayList<V> values)
    {
        super(keys, vals);
        this.cmp = new Comparator<K>();
    }

    // Use the given comparator to sort the keys
    public FastGetListMM(Comparator<K> cmp)
    {
        super(cmp);
        //this.cmp = cmp;
    }

    @Override
    public int indexOf(K key) {

        return 0;
    }

    @Override
    public V put(K key, V value) {

        return null;
    }

    @Override
    public int compare(K arg0, K arg1) {
        // TODO Auto-generated method stub
        return 0;
    }

}

SimpleListMM class:

import java.util.*;

public class SimpleListMM<K,V> extends AbstractListMM<K,V> {

    //protected ArrayList<K> keys;
    //protected ArrayList<V> vals;

    // No special parameters required
    public SimpleListMM(ArrayList<K> keys, ArrayList<V> vals)
    {
        super(keys, vals);
    }

    // Scan through the list of keys linearly searching for the given
    // key. If not present, return a negative number.
    public int indexOf(K key)
    {
        K index = null;
        for(int i = 0; i < keys.size(); i++)
        {
            if(keys.get(i) != key)
                return -1;
            else 
                index = keys.get(i);
        }
        return (Integer) index;
    }

    // Locate the given key and replace its binding with the given
    // value. If not present, add the key and value onto the end of
    // their respective lists.
    public V put(K key, V value)
    {
        for(int i = 0; i < keys.size(); i++)
        {
            if(keys.get(i) == key)
                vals.set((Integer)keys.get(i), value);
            else
            {
                keys.add(key);
                vals.add(value);
            }
        }
        return (V)vals;
    }
}

Upvotes: 0

Views: 202

Answers (1)

Luiggi Mendoza
Luiggi Mendoza

Reputation: 85779

Your classes don't have a no-arg constructor, thus the compiler error. Add the proper constructor in both classes:

public class SimpleListMM<K,V> extends AbstractListMM<K,V> {

    public SimpleListMM() {
        //some initialization logic
        //maybe like this
        super(new ArrayList<K>(), new ArrayList<V>());
    }

    public SimpleListMM(ArrayList<K> keys, ArrayList<V> vals) {
        super(keys, vals);
    }
}

Upvotes: 1

Related Questions