Article

The JSP Files - Parts 1 to 8: Tagged and Bagged

Page: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 Next

What's For Dessert?

Thus far, the variables you've used contain only a single value - for example,

int i = 0

However, array variables are a different kettle of fish altogether. An array variable can best be thought of as a "container" variable, which can contain one or more values. For example,

String[] desserts = {"chocolate mousse", "tiramisu",                    
"apple pie", "chocolate fudge cake"};

Here, "desserts" is an array variable, which contains the values "chocolate mousse", "tiramisu", "apple pie", and "chocolate fudge cake".

Array variables are particularly useful for grouping related values together - names, dates, phone numbers of ex-girlfriends et al.

The various elements of the array are accessed via an index number, with the first element starting at zero. So, to access the element

"chocolate mousse"

you would use the notation

desserts[0]

while

"chocolate fudge cake"

would be

desserts[3]

- essentially, the array variable name followed by the index number enclosed within square braces. Geeks refer to this as "zero-based indexing".

Defining an array variable in JSP is somewhat convoluted, as compared to languages like PHP and Perl; you need to first declare the variable and its type, and then actually create the array.

<%                  
// declare type of array                  
String[] desserts;                  
                 
// initialize array                  
// the number indicates the number of elements the array will                    
hold desserts = new String[4];                  
                 
// assign values to array elements                  
desserts[0] = "chocolate mousse";                  
desserts[1] = "tiramisu";                  
desserts[2] = "apple pie";                  
desserts[3] = "chocolate fudge cake";                  
%>

Or you can use the simpler version:

<%                  
String[] desserts = {"chocolate mousse", "tiramisu",                    
"apple pie", "chocolate fudge cake"}; %>

Note that if you try to add more elements than the number specified when creating the array, JSP will barf with a series of strange error message.

A Chocolate Addiction

In order to modify an element of an array, you would simply assign a new value to the corresponding variable. If you wanted to replace "chocolate fudge cake" with "chocolate chip cookies", you'd simply use

<%                  
desserts[3] = "chocolate chip cookies";                  
%>

and the array would now read

String[] desserts = {"chocolate mousse", "tiramisu",                    
"apple pie", "chocolate chip cookies"};

JSP arrays can only store the type of data specified at the time of declaring the array variable; a string array cannot hold numbers, or vice-versa.

And finally, you can obtain the number of items in an array with the "length" property - the following example demonstrates this:

<html>                  
<head>                  
<basefont face="Arial">                  
</head>                  
                 
<body>                  
<%                  
// define an array                  
String[] desserts = {"chocolate mousse", "tiramisu",                    
"apple pie", "chocolate fudge cake"};                  
                 
// display array length                  
out.println("The dessert menu                    
contains " + desserts.length + " items.");  %>  </body> </html>

And the output is:

The dessert menu contains 4 items.

You can use a "for" loop to iterate through the elements of an array, as the following example demonstrates.

<html>                  
<head>                  
<basefont face="Arial">                  
</head>                  
                 
<body>                  
Desserts available:                  
<ul>                  
                 
<%                  
// define counter                  
int counter;                  
                 
// define an array                  
String[] desserts = {"chocolate mousse", "tiramisu",                    
"apple pie", "chocolate fudge cake"};                  
                 
for (counter=0; counter<desserts.length; counter++)                  
{                  
out.println("<li>" + desserts[counter] + "<br>");                  
}                  
%>                  
                 
</ul>                  
</body>                  
</html>

And the output is:

Desserts available:                  
· chocolate mousse                  
· tiramisu                  
· apple pie                  
· chocolate fudge cake

Copyright Melonfire, 2000. All rights reserved.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links