繁体   English   中英

使用数组实现地图

[英]Implementing Map using Arrays

我想使用数组实现Map Data结构。

我有兴趣创建两种方法。 下面是我的代码结构:

public class ImplementMap<K,V> {

    public void insert(K key, V value) { //to insert string

    }

    public V retrieve(K key) {//to retrieve value if key is given

    }

}

例如 :

ImplementMap<String, String> newMap = new ImplementMap<>();

newMap.insert("IN", "India");

newMap.retrieve("IN");  India //Output should be India

我收到错误消息

第37行中的“在此令牌后预期的令牌“。”上的语法错误”。我已在代码中注释了第37行。

请帮助我获得正确的输出。 这是我的代码:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ImplementMap<K, V> {

    public void insert(K key[], V value[], int K, Object V) { //inserts 'IN' 'India'
        ArrayList<Object> newInsertion = new ArrayList<>();
        newInsertion.add(K, V); 
        System.out.println(newInsertion);


    }

   public void retrieve(K key[], int K){//has to retrieve 'India' 
     int[] index = null;
     int retrievevalue;
    @SuppressWarnings("null")
    int findindex = index[K];
      retrievevalue = findindex + 1;
    System.out.println(retrievevalue);



}


ImplementMap<String, String> newmap = new ImplementMap<>();

newmap.insert("IN", "India"); // this is line 37 
newmap.retrieve("IN";
}

您需要将该代码包装在一个方法中。 要测试最简单的方法是添加“ main”方法

public static void main(String[] args) {
    ImplementMap<String, String> newmap = new ImplementMap<>();

    newmap.insert("IN", "India"); // this is line 37 
    newmap.retrieve("IN");
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM