关于Hashtable的问题,up有分

offsider 2002-08-20 03:39:01
大家看看这段jsp代码是不是有问题?为什么我书读到页面的两个Hashtable的大小一样呢?

<%@page import="java.util.*"%>

<%
Hashtable test1 = new Hashtable();
Hashtable all = new Hashtable();
test1.put("A","第一列");
test1.put("B","第二列");
all.put("FIRST",test1);
test1.clear();
test1.put("1","No1");
test1.put("2","No2");
test1.put("3","No3");
all.put("SECOND",test1);
out.println("all.size = "+all.size()+"<br>");
Hashtable check = (Hashtable)all.get("FIRST");
out.println("first.size = "+check.size()+"<br>");
Hashtable check1 = (Hashtable)all.get("SECOND");
out.println("second.size = "+check1.size()+"<br>");
%>

运行结果如下:
all.size = 2
first.size = 3
second.size = 3
但我觉得应该是
all.size = 2
first.size = 2 //应该是2才对
second.size = 3
请各位高手指点,
...全文
34 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Andrawu 2002-08-20
  • 打赏
  • 举报
回复
try:

<%@page import="java.util.*"%>

<%
Hashtable test1 = null ;
Hashtable all = new Hashtable();
test1 = new Hashtable() ;
test1.put("A","第一列");
test1.put("B","第二列");
all.put("FIRST",test1);
test1 = new Hashtable() ;
test1.put("1","No1");
test1.put("2","No2");
test1.put("3","No3");
all.put("SECOND",test1);
out.println("all.size = "+all.size()+"<br>");
Hashtable check = (Hashtable)all.get("FIRST");
out.println("first.size = "+check.size()+"<br>");
Hashtable check1 = (Hashtable)all.get("SECOND");
out.println("second.size = "+check1.size()+"<br>");
%>
Amani 2002-08-20
  • 打赏
  • 举报
回复
第二次又test1.put了
goodflash 2002-08-20
  • 打赏
  • 举报
回复
up
dagutou 2002-08-20
  • 打赏
  • 举报
回复
test1.clear();
会清除所有内容

Amani 2002-08-20
  • 打赏
  • 举报
回复
<%
Hashtable test1 = new Hashtable();
Hashtable test2 = new Hashtable();
Hashtable all = new Hashtable();
test1.put("A","第一列");
test1.put("B","第二列");
all.put("FIRST",test1);
test2.put("1","No1");
test2.put("2","No2");
test2.put("3","No3");
all.put("SECOND",test2);
out.println("all.size = "+all.size()+"<br>");
Hashtable check = (Hashtable)all.get("FIRST");
out.println("first.size = "+check.size()+"<br>");
Hashtable check1 = (Hashtable)all.get("SECOND");
out.println("second.size = "+check1.size()+"<br>");
%>
运行结果应该是

all.size = 2
first.size = 2
second.size = 3

我想问题出在test1上,有可能都指向test1,才得出
all.size = 2
first.size = 3
second.size = 3


81,092

社区成员

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

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