473,387 Members | 1,844 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,387 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 11224
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.