473,396 Members | 2,014 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Two dimensional array + c:foreach

Hello

I have a servlet that creates results in a two dimentional array and then uses request.setAttribute to store it.

for example :
String[][] fullname = {{"name1", "surname1"},{"name2", "surname2"},{"name3", "surname3"}};

request.setAttribute("fullname",fullname);

Then JSP page will use JSTL c:foreach to display it:

<c:forEach var = "itemarr2" items = "${fullname}">
<tr>

<td>${itemarr2[0]}</td>
<td>${itemarr2[1]}</td>
</tr>
</c:forEach>

The above is working fine, except that in this case I know the array has two columns, but in many cases I don't know how many columns I have. Is there a way to make column numbers dynamic ?

Or may be another tag or method to solve this problem ?

Thank you
Lisa
Aug 17 '07 #1
3 11226
JosAH
11,448 Expert 8TB
Hello

I have a servlet that creates results in a two dimentional array and then uses request.setAttribute to store it.

for example :
String[][] fullname = {{"name1", "surname1"},{"name2", "surname2"},{"name3", "surname3"}};

request.setAttribute("fullname",fullname);

Then JSP page will use JSTL c:foreach to display it:

<c:forEach var = "itemarr2" items = "${fullname}">
<tr>

<td>${itemarr2[0]}</td>
<td>${itemarr2[1]}</td>
</tr>
</c:forEach>

The above is working fine, except that in this case I know the array has two columns, but in many cases I don't know how many columns I have. Is there a way to make column numbers dynamic ?

Or may be another tag or method to solve this problem ?

Thank you
Lisa
Well, you do know that itemarr2 is an array so you can use plain old Java to get
the number of elements in that array: itemarr2.length. You can use plain old
loops if you want or the JSTL loop.

kind regards,

Jos
Aug 17 '07 #2
Well, you do know that itemarr2 is an array so you can use plain old Java to get
the number of elements in that array: itemarr2.length. You can use plain old
loops if you want or the JSTL loop.

kind regards,

Jos

Thank you for your answer ..... Could you kindly give a hint on how to use itemarr2.length inside the c:foreach to accomplish this ?

Any examples on how to implement this is extremely appreciated.

Lisa
Aug 17 '07 #3
JosAH
11,448 Expert 8TB
Thank you for your answer ..... Could you kindly give a hint on how to use itemarr2.length inside the c:foreach to accomplish this ?

Any examples on how to implement this is extremely appreciated.

Lisa
I don't speak JSP JSTL tags, but I guess it can be something like this:

Expand|Select|Wrap|Line Numbers
  1. <c:forEach var = "name" items = "${itemarr2}">
  2. // do something with name here
  3.  
kind regards,

Jos
Aug 17 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Malcolm | last post by:
I want to put the contents of a csv file into a 2 dimensional array so that I can access any element in a $table manner and have come up with the following code: $complete = file("source.csv");...
3
by: Stefan Richter | last post by:
Hi, I want to go create a table with a textfield where the user can enter an amount for a certain dynamically created article coming from the database. Later, I want to calculate the whole price...
5
by: news | last post by:
Using php.net I got this far, and so far so good! But I can't fenagle it quite the way I need to. I'm creating an array (which I didn't know how to do before today, so a success for me *g*): ...
8
by: Gopal Krish | last post by:
Any sample code that uses foreach statement to walkthrough a two dimensional array? My requirement is to process data in only one dimension (say column 0 in each row of the array) Example...
8
by: per9000 | last post by:
Hi all, I have a two-dimensional array of data, f.x int's. We can imagine that the array is "really large". Now I want the data in it and store this in a one-dimensional array. The obvious...
1
khalidbaloch
by: khalidbaloch | last post by:
hi every one, how are you folf , hope fine dear Friends i want to get values of multi-dimensional arrays using foreach loop and after that print out the html using an other while loop , i tried...
4
by: Sandman | last post by:
Hi, So I have 2 arrays: one contains userids. It may look like: user_id =12, user_id =30, user_id =43 The other is a multi-dimensional array with fields like: user_info = Array (
5
by: Mukesh | last post by:
Hi, I am using framework 2.0. I am writing a foreach loop that will extract single dimensional arrays out of double dimensional array. I am trying writing something like this. string ...
6
by: johntology | last post by:
Hello, I've made a two dimensional array using references, which I gather is the only way to do it in Perl. I now need to pass each interior array to a subroutine for processing and can't quite...
110
by: fjm | last post by:
For some reason, I have always had a hard time understanding arrays as they pertain to php and databases. I understand associative arrays just fine but when there are multidimensional arrays, I kinda...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.