简体   繁体   中英

Implementing Map using Arrays

I want to implement Map Data structure using Arrays.

I'm interested to create two methods. Below is my code structure:

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

    }

}

For example :

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

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

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

I'm getting error as

"Syntax error on token "." expected after this token" in line 37. I have commented line 37 in code.

Please help me to get right output. This is my code :

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";
}

You need to wrap that code in a method. For testing the easiest thing to do is add a "main" method

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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