0

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

1 Answer 1

1

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");
}
Sign up to request clarification or add additional context in comments.

11 Comments

The method insert(String[], String[], int, Object) in the type ImplementMap<String,String> is not applicable for the arguments (String, String) The method retrieve(String[], int) in the type ImplementMap<String,String> is not applicable for the arguments (String)
You need to call your methods with the same arguments that they require. You've written your "insert" method so that it requires and array of K elements, an array of V elements, an integer and an object, but you're only handing it two parameters in the call, a string and a string
in fact, it looks like you've changed your method signatures from what the initial design was. Without knowing the lesson, I think you need to change them back to what you had initially
Hi mike, I can insert the data, but I am still trying to retrieving the data.
public void insert(String K, String V) { //inserts 'IN' 'India' ArrayList<Object> newInsertion = new ArrayList<>(); newInsertion.add(K); newInsertion.add(V); System.out.println(newInsertion); }
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.