gpt4 book ai didi

java - 使用数组实现 Map

转载 作者:行者123 更新时间:2023-12-02 13:41:24 25 4
gpt4 key购买 nike

我想使用数组实现 map 数据结构。

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

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

我收到错误为

"Syntax error on token "." expected after this token" in line 37. I have commented line 37 in 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";
}

最佳答案

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

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

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

关于java - 使用数组实现 Map,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42722834/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com