本文共 2065 字,大约阅读时间需要 6 分钟。
put() method is available in java.util package.
put()方法在java.util包中可用。
put() method is used to put the given key to the given value in this dictionary.
put()方法用于将给定键放入此字典中的给定值。
put() method is a non-static method, it is accessible with the class object and if we try to access the method with the class name then we will get an error.
put()方法是一个非静态方法,可通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。
put() method may throw an exception at the time of returning value associated with the key.
put()方法在返回与键关联的值时可能会引发异常。
NullPointerException: This exception may throw when the given any of the parameters is null exists.
NullPointerException :当给定的任何参数为null时,可能引发此异常。
Syntax:
句法:
public abstract Value put(Key k, Value val);
Parameter(s):
参数:
Key k – represents the key value(k).
密钥k –代表密钥值(k)。
Value val – represents the value(val).
值val –表示值(值)。
Return value:
返回值:
The return type of this method is Value, it gets value associated with the given key(k) otherwise it returns null when the key did not hold any value.
此方法的返回类型为Value ,它获取与给定键(k)关联的值 ,否则当键不包含任何值时返回null。
Example:
例:
// Java program is to demonstrate the example of// put(Key k, Value val) method of Dictionaryimport java.util.*;public class PutOfDictionary { public static void main(String[] args) { // Instantiate a Hashtable object Dictionary dictionary = new Hashtable(); // By using put() method is to // add an elements in Hashtable dictionary.put(1, "C"); dictionary.put(2, "C++"); dictionary.put(3, "JAVA"); dictionary.put(4, "PHP"); dictionary.put(5, "SFDC"); //Display dictionary System.out.println("dictionary: " + dictionary); // Here we are replacing the value exists // on the given key element 3 dictionary.put(3, "Microservices"); // Display Modified Dictionary System.out.println("dictionary: " + dictionary); }}
Output
输出量
dictionary: {5=SFDC, 4=PHP, 3=JAVA, 2=C++, 1=C}dictionary: {5=SFDC, 4=PHP, 3=Microservices, 2=C++, 1=C}
翻译自:
转载地址:http://ljvzd.baihongyu.com/