如何创建和填充哈希表的 Java 数组?


创建哈希表数组的一种方法是创建哈希表对象,并使用花括号将其分配给哈希表数组

示例

在线演示

import java.util.Hashtable;

public class HashTableOfArrays {
   public static void main(String args[]) {
      Hashtable<String, String> ht1 = new Hashtable();
      ht1.put("Name", "Krishna");
      ht1.put("Age", "28");
      ht1.put("City", "Visakhapatnam");
      ht1.put("Phone", "9848022338");
     
      Hashtable<String, String> ht2 = new Hashtable();
      ht2.put("Name", "Raju");
      ht2.put("Age", "30");
      ht2.put("City", "Chennai");
      ht2.put("Phone", "9848033228");
     
      Hashtable<String, String> ht3 = new Hashtable();
      ht3.put("Name", "Satish");
      ht3.put("Age", "35");
      ht3.put("City", "Hyderabad");
      ht3.put("Phone", "9848023848");
      Hashtable [] hashArray = {ht1, ht2, ht3};
     
      for(int i = 0; i<hashArray.length; i++){
         System.out.println(hashArray[i]);
      }
   }
}

输出

{Name=Krishna, City=Visakhapatnam, Phone=9848022338, Age=28}
{Name=Raju, City=Chennai, Phone=9848033228, Age=30}
{Name=Satish, City=Hyderabad, Phone=9848023848, Age=35}

更新日期:16-6-2020

533 次浏览

开启您的 职业生涯

完成课程并获得认证

开始
广告