我无法准确理解我需要为这个项目做些什么!不要求任何代码因为我希望自己能够做到这一点。
“在Java中将给定的List接口实现为数组列表。”
鉴于此代码示例:
public interface List<K extends Comparable<K>,V> {
public abstract boolean add(K key,V value);
public abstract V remove(K key);
public abstract V remove(int n);
public abstract V remove();
public abstract V lookup(K key);
public abstract int size();
public abstract V get(int n);
}
方向有点模糊,我该如何处理?
答案 0 :(得分:1)
基本上Comparable接口允许实现它的类可以被排序和其他比较机制,比如比较字符串,检查两个对象是否包含相同的数据等。在这种情况下,它的Generic类K单独是Comparable(来自代码行
K extends Comparable < K >
如果我是对的,则会要求您实现一个名为“List”的接口,该接口可以容纳一些“Key-Value”对。并且要求您编写实现这些方法的类。按键删除,按值删除,删除全部,大小等。所以你可以从一个可以容纳这些对的数组开始。
希望这可能对你有用。
答案 1 :(得分:0)
您需要做的是编写一个实现上述接口的新类。这个新类应该像Java ArrayList类一样。在这个类中,您应该使用一些数据结构来实现所有方法。显然,您不应该使用Java ArrayList作为默认数据结构。 (提示:可能是一个数组)。
public class MyArrayList implements List{
Object[] objectArray;
public boolean add(){
}
}
这是一个编程练习,可以让你提高你的java技能。
答案 2 :(得分:0)
无论谁编写该作业,都应该在方法中加入Javadoc,这样你就可以知道它们应该做什么了。
首先,界面命名错误,因为它的行为更像Map
而不是List
。由于只有一个add()
方法,并且你有两个(三个?)方法接受一个索引参数,并且赋值说“像一个数组列表”,我建议你实现add()
的方式LinkedHashMap
会这样做,因为该类是Map
,它会按照ArrayList
的方式保留广告订单。
您的实现应该可以在内部KeyValuePair
类的数组内部存储键/值对。由于没有关于性能的要求,具有键值的方法应该只进行顺序搜索。
更新替代理论:密钥类型K
定义为extends Comparable<K>
的原因是,您可以调用key1.compareTo(key2)
,或者更准确地说,因此数组可以排序,您可以进行二进制搜索以找到密钥。这意味着它的行为更像TreeMap
而不是LinkedHashMap
。否则,我真的没有找到密钥需要Comparable
的原因。
所以,这里是来自LinkedHashMap
和ArrayList
的简化javadoc的接口,它假定了插入顺序,而不是排序:
/**
* This list defines the iteration ordering, which is normally the
* order in which keys were inserted into the list (insertion-order).
* Note that insertion order is not affected if a key is re-inserted into the list.
*/
public interface List<K extends Comparable<K>,V> {
/**
* Associates the specified value with the specified key in this map.
* If the map previously contained a mapping for the key, the old value is replaced.
*
* @return the previous value associated with key, or null if there was no mapping for key.
*/
public abstract boolean add(K key,V value);
/**
* Removes the mapping for the specified key from this map if present.
*
* @return the previous value associated with key,
* or null if there was no mapping for key.
*/
public abstract V remove(K key);
/**
* Removes the element at the specified position in this list.
* Shifts any subsequent elements to the left (subtracts one from their indices).
*
* @return the element that was removed from the list
* @throws IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())
*/
public abstract V remove(int n);
/**
* Removes the first element in this list.
* Same as remove(0).
*/
public abstract V remove();
/**
* Returns the value to which the specified key is mapped,
* or null if this map contains no mapping for the key.
*/
public abstract V lookup(K key);
/**
* Returns the number of key-value mappings in this list.
*/
public abstract int size();
/**
* Returns the element at the specified position in this list.
*
* @throws IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())
*/
public abstract V get(int n);
}