Java 使用数组实现Map
我想使用数组实现地图数据结构 我有兴趣创建两种方法。下面是我的代码结构:Java 使用数组实现Map,java,arrays,Java,Arrays,我想使用数组实现地图数据结构 我有兴趣创建两种方法。下面是我的代码结构: 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 } } 公共类实现映射{ 公共void insert(K键,V值
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
}
}
公共类实现映射{
公共void insert(K键,V值){//插入字符串
}
public V retrieve(K key){//如果给定key,则检索值
}
}
例如:
ImplementMap<String, String> newMap = new ImplementMap<>();
newMap.insert("IN", "India");
newMap.retrieve("IN"); India //Output should be India
ImplementMap newMap=newimplementmap();
新增地图。插入(“在”、“印度”);
newMap.retrieve(“IN”);印度//产出应该是印度
我得到了错误的答案
第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";
}
import java.util.ArrayList;
导入java.util.array;
导入java.util.List;
公共类实现映射{
public void insert(K键[],V值[],int K,对象V){//inserts'在“”印度'
ArrayList newInsertion=新ArrayList();
添加(K,V);
System.out.println(新插入);
}
public void检索(K key[],int K){//必须检索'India'
int[]索引=null;
int检索值;
@抑制警告(“空”)
int findindex=索引[K];
retrievevalue=findindex+1;
System.out.println(检索值);
}
ImplementMap newmap=新ImplementMap();
newmap.insert(“IN”,“India”);//这是第37行
检索(“IN”;
}
您需要将该代码包装到一个方法中。对于测试,最简单的方法是添加一个“main”方法
publicstaticvoidmain(字符串[]args){
ImplementMap newmap=新ImplementMap();
newmap.insert(“IN”,“India”);//这是第37行
newmap.retrieve(“IN”);
}
类型ImplementMap中的方法insert(String[],String[],int,Object)不适用于参数(String,String)。类型ImplementMap中的方法retrieve(String[],int)不适用于需要使用所需参数调用方法的参数(String)。您已经编写了“insert”方法,因此它需要和K元素数组、V元素数组、整数和对象,但您在调用中只处理了两个参数,一个字符串和一个字符串。事实上,看起来您已经改变了最初设计的方法签名。在不了解课程的情况下,我想您需要将它们更改回你的名字是initiallyHi mike,我可以插入数据,但我仍在尝试检索数据。public void insert(字符串K,字符串V){//inserts'IN''India'ArrayList newInsertion=new arrarylist();newInsertion.add(K);newInsertion.add(V);System.out.println(newInsertion);}
public static void main(String[] args) {
ImplementMap<String, String> newmap = new ImplementMap<>();
newmap.insert("IN", "India"); // this is line 37
newmap.retrieve("IN");
}