import java.util.HashMap;
public class Map {
public static void main(String[] args) {
// TODO Auto-generated method stub
int arr[] = {10, 3, 34, 3, 10};
HashMap<Integer, Integer> hmap=new HashMap<Integer, Integer>();
for (int i = 0; i < arr.length; i++) {
System.out.println(hmap.put(arr[i], 1));
}
}
}
我得到的输出如下:为什么我得到这个Hashmap输出?
null
null
null
1
1
我没有得到为什么我得到的输出作为三次null
和两次之一。
为什么最初它的输出为空 – Swati
因为'put()'函数[定义为返回与键关联的旧值](https://docs.oracle.com/javase/8/docs/ API/JAVA/UTIL/HashMap.html#把-KV-)。 –
@安迪托马斯,如果你解释一些更多的话,这将是非常棒的。 – Swati