我定义了一个Hashtable对象 它的key,  value对象我存的都是数字的字符串 例如“123”
我怎么将Hashtable的key和value的值取出来 存放到一个byte[] 中

解决方案 »

  1.   

    public static void main(String[] args) {
    Hashtable ht = new Hashtable();
    ht.put("1", "123");
    ht.put("2", "456");
    ht.put("3", "789");

    Enumeration e = ht.elements();
    while (e.hasMoreElements()) {
    String s = e.nextElement().toString();
    int len = s.length();
    byte[] b = new byte[len];
    for (int i = 0; i < len; i++) {
    b[i] = (byte)s.charAt(i);
    }
    }
    }此方法很笨。不过可以实现功能。
    我记得好像有一个函数可以以string转成char[]数组来的。找不到了。
      

  2.   

    getBytes() 不过楼住的题目不是很明确
      

  3.   

    public static void main(String[] args)
      {
        HashMap map = new HashMap();
        map.put("1","123");
        map.put("2","234");
        
        StringBuffer sb = new StringBuffer();
        Iterator iter = map.keySet().iterator();
        while( iter.hasNext() )
        {
          String key = (String)iter.next();
          String value = (String)map.get(key);
          sb.append(key);
          sb.append(value);
        }
        byte[] result = sb.toString().getBytes();
        for(int i=0; null!=result && i<result.length; i++)
        {
          System.out.println((char)result[i]);
        }
        
      }试试先,能满足要求否?