I have an Arraylist of Arraylist< String>'s and I am trying to access a string value. Say I wanted to access the third String in the second ArrayList in my jsp file, but I wanted to do it without scripting, using EL. would this be correct? ${anArrayList[2][3]}
Asked
Active
Viewed 923 times
1 Answers
0
Almost correct. Array indexes start with 0
, not 1
as you seem to think. This has always been the case in normal Java code and this is not different in EL. So, to get the second list and then the third string, you need respectively the indexes 1
and 2
. So, this should do
${anArrayList[1][2]}
...assuming that you've already placed ${anArrayList}
in the desired scope. For example, in the request scope with help of a preprocessing servlet:
List<List<String>> anArrayList = createItSomehow();
request.setAttribute("anArrayList", anArrayList);
// ...
// I'd invent a more self-documenting variable and attribute name though.
By the way, are you familiar with Javabeans? This smells much that you rather need a List<Entity>
.
-
Sorry that was silly mistake, I know array indexes start as 0 and those are not my real var names. I have little experience with javabeans but I don't know what an Entity is, could you explain a little more? In any case, I think that for my purposes arraylist of arraylists would be fine, but if this is awkward coding practice i would like to learn more about how I would use a List< entity> – joseph Jul 30 '11 at 04:19
-
It was just an abstract example. Think of a single class which represents a real world entity which contains all properties which you've all put in the 2d list. Maybe `Person` with `id`, `firstname`, `Address`, etc? Or `Product` with `id`, `description`, `price`, etc? Or maybe `Order` with `id`, `Customer`, `quantity`, etc? Like as a single row of a well-designed database table. See also the link behind "Javabeans" in my answer. – BalusC Jul 30 '11 at 04:59
-
I did think about that but I decided not to create a "Person" or similar class at all. I am not sure if this is bad programming practice but it seemed to me as though just doing it without using a class would be simpler and avoid extra code. However, I guess it would look less ugly. – joseph Jul 31 '11 at 07:31