Java 根据jstl中的索引获取arraylist的元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24019845/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting elements of arraylist based on index in jstl
提问by tawheed
This is perhaps a relatively simple thing to do but for some reason I don't seem to get it right.
这可能是一件相对简单的事情,但出于某种原因,我似乎没有做对。
How does one get an element from an arrayList in jstl based on an index.
如何根据索引从 jstl 中的 arrayList 中获取元素。
In pure java, lets say I have this arralist
在纯 Java 中,可以说我有这个 arralist
ArrayList< String > colors = new ArrayList< String >();
colors.add("red");
colors.add("orange");
colors.add("yellow");
colors.add("green");
colors.add("blue");
if I do System.out.println(colors.get(1));
I get the first color from the arrayList based
on the index I supplied which happens to be red
.
如果我这样做,System.out.println(colors.get(1));
我会根据我提供的索引从 arrayList 中获取第一种颜色,该索引恰好是red
.
Wondering how to achieve that in jstl. I played with the foreach tag in jstl but did not quite get it right.
想知道如何在jstl中实现这一点。我在 jstl 中使用了 foreach 标签,但没有完全正确。
<c:forEach items="colors" var="element">
<c:out value="${element.get(1)}"/>
</c:forEach>
采纳答案by Syam S
When you say colors.get(1);
or ${element[1]}
its actually referrign to single entry in the list. But when you use c:forEach
its iterating the loop.
It depends on what you are trying to achieve. If you just want the Nth element try
当你说colors.get(1);
或${element[1]}
它实际上是指列表中的单个条目时。但是当你使用c:forEach
它迭代循环时。这取决于您要实现的目标。如果您只想要第 N 个元素,请尝试
<c:out value="${colors[1]}"/> // This prints the second element of the list
but rather you want to print the entire elements you should go for loop like
而是你想打印你应该去循环的整个元素
<c:forEach items="${colors}" var="element">
<c:out value="${element}"/>
</c:forEach>
Also please note if you write like <c:forEach items="colors" var="element">
it literally treat the value as "colors"
. So if its a variable name you need to give it in ${}
like ${colors}
as depicted in example above.
另请注意,如果您像<c:forEach items="colors" var="element">
它一样写字 面上将值视为"colors"
. 所以如果它是一个变量名,你需要${}
像${colors}
上面的例子中描述的那样给出它。
回答by lpratlong
This should work:
这应该有效:
<c:out value="${colors[0]}"/>
It will print you "red".
它会给你打印“红色”。
But if you want to print all the values of your list, you can use your foreach
like that:
但是,如果您想打印列表中的所有值,您可以foreach
像这样使用:
<c:forEach items="colors" var="element">
<c:out value="${element}"/>
</c:forEach>
This code will iterate over your list colors
and set each element of this list in a variable called element
. So, element
has the same type as the one who parameterized your list (here, String
). So you can not call get(1)
on String
since it does not exist.
So you call directly <c:out value="${element}"/>
which will call toString()
method of your current element.
此代码将遍历您的列表colors
并将此列表的每个元素设置在名为element
. 因此,element
具有与参数化列表的类型相同的类型(此处为String
)。所以你不能调用get(1)
,String
因为它不存在。所以你直接调用<c:out value="${element}"/>
它会调用toString()
你当前元素的方法。
回答by Beau Grantham
You can used array-based access as follows:
您可以使用基于数组的访问,如下所示:
<c:out value="${colors[0]}" />