Java 在jsp中使用嵌套数组迭代和打印HashMap的内容
我用以下方式创建了一个HashMapJava 在jsp中使用嵌套数组迭代和打印HashMap的内容,java,javascript,arrays,jsp,hashmap,Java,Javascript,Arrays,Jsp,Hashmap,我用以下方式创建了一个HashMap HashMap products = new HashMap<String, String[]>(); products.put("001",new String[] {"SAM", "100"}); HashMap products=newhashmap(); products.put(“001”,新字符串[]{“SAM”,“100”}); 现在我需要在一个表中打印映射的内容,我知道如何打印它,如果它是用嵌套数组创建的,如下面的代码片段所示
HashMap products = new HashMap<String, String[]>();
products.put("001",new String[] {"SAM", "100"});
HashMap products=newhashmap();
products.put(“001”,新字符串[]{“SAM”,“100”});
现在我需要在一个表中打印映射的内容,我知道如何打印它,如果它是用嵌套数组创建的,如下面的代码片段所示
<%
for (Map.Entry<String, String> entry : orderList.entrySet()) {
%>
<tr>
<td><%=counter++%></td>
<td><%=entry.getKey()%></td>
<td><%=entry.getValue()%></td>
</tr>
如何使用嵌套数组打印HashMap中的内容
我试过的解决方案
<%
for (Map.Entry<String, String[]> entry : Order.entrySet()) {
%>
<tr>
<td><%=counter++%></td>
<td><%=entry.getKey()%></td>
<td></td>
<td></td>
</tr>
<%
您还需要一个嵌套循环
for (String arrayElement: entry.getValue()) {
<%=arrayElement%>
for(字符串数组元素:entry.getValue()){
您可以在
中定义嵌套表,而不是简单的
为什么我们不能在这里使用jstl?比如
<c:forEach var="entry" items="${products}">
Key: <c:out value="${entry.key}"/>
Value: <c:forEach var="arrayVar" items="${entry.value}">
<li>${arrayVar}</li>
</c:forEach>
</c:forEach>
关键:
价值:
${arrayVar}
由于StanislavL和阴影部分的回答,我成功地获得了解决方案,因此完整的解决方案如下图所示
HashMap的定义如下:
static HashMap<String,String[]> products = new HashMap<String, String[]>();
products.put("001",new String[] {"Samsung", "USD. 500 ", "5 Units" });
static HashMap products=new HashMap();
产品.put(“001”,新字符串[]{“三星”,“500美元”,“5个单位”);
可以使用JSP将此Hashmap打印在如下所示的表中
<% int counter = 1; %>
<table class="styledLeft" id="moduleTable">
<thead>
<tr>
<th width="10%">No</th>
<th width="10%">Model No</th>
<th width="30%">Model/Make</th>
<th width="30%">Price</th>
<th width="20%">Available Quantity</th>
</tr>
</thead>
<tbody>
<%
for (Map.Entry<String, String[]> entry : orders.entrySet()) {
%>
<tr>
<td><%=counter++%></td>
<td><%=entry.getKey()%></td>
<%for (String arrayElement: entry.getValue()) {%>
<td><%=arrayElement%></td>
<%
}
%>
</tr>
<%
}
%>
</tbody>
</table>
不
型号
型号/品牌
价格
可用数量
最终输出如下所示
您是否尝试过使用Map.Entry:orderList.entrySet()呢
?如果您将上面的我的代码和下面的@StanislavL的代码结合起来,您应该有自己的解决方案。如果不是这样,您需要发布出了什么问题。它没有打印吗?它是否给出了错误?是的,谢谢您的回答。在这种情况下,定义第一个循环的方法是什么?我尝试了映射。条目:orderList.entrySet();它不起作用你定义了产品映射,但你定义了一些未知的订单列表。订单列表是什么?你说“它不起作用”.哪种方式不起作用?有错误吗?结果吗?嗨,这只是一个输入错误,我尝试的解决方案被添加到原始问题中。谢谢你的帮助answer@Luiggi..不。我在这里迭代数组并打印结果。所以它会打印值。如果我们给出像
这样的值,那么它会像你提到的那样打印。哦,对了。没有看到内部<代码>
。那么这就是要走的路。