遇到繁琐的问题!

zhonglizhi 2005-11-28 10:15:19
我定义了一个Hashtable对象 它的key, value对象我存的都是数字的字符串 例如“123”
我怎么将Hashtable的key和value的值取出来 存放到一个byte[] 中
...全文
92 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
niko7 2005-11-28
  • 打赏
  • 举报
回复
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]);
}

}

试试先,能满足要求否?
xf_luo 2005-11-28
  • 打赏
  • 举报
回复
getBytes()

不过楼住的题目不是很明确

skycncomp 2005-11-28
  • 打赏
  • 举报
回复
你要记得给分啊
skycncomp 2005-11-28
  • 打赏
  • 举报
回复
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[]数组来的。找不到了。
zhonglizhi 2005-11-28
  • 打赏
  • 举报
回复
谁教教我呀

62,629

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧