如何在给定索引处插入给定的键值对?

时间:2016-04-14 04:40:45

标签: java data-structures symbol-table

如何将键值对插入特定索引的数组中?这是我到目前为止的代码,但它没有通过我的测试。它失败了:

Fails on the below key value pairs:
size = 1, key-value pairs = (ZS, 160)
Failure in putting (ZS, 160) into the following symbol table:
size = 0, key-value pairs = 
public class SortedArrayST<Key extends Comparable<Key>, Value> {
    private static final int MIN_SIZE = 2;
    private Key[] keys;      // the keys array
    private Value[] vals;    // the values array
    private int N = 0;       // size of the symbol table


    private void insert(Key key, Value val, int r) {
        // TODO
        //shift to the left
        if (keys == null) return;
        int i = rank(keys[r]);
        if (i == N || keys[i].compareTo(keys[r]) != 0){
            return;
        }
        for (int j = i; j < N-1; j++)  {
            keys[j] = keys[j+1];
            vals[j] = vals[j+1];
        }
            N--;
             keys[N] = null;
             vals[N] = null;        
}}

0 个答案:

没有答案