Jsp array definition, traversing the output method

Source: Internet
Author: User
Tags array definition arrays

An array defines a vector for an array that does not know the length at the beginning. It is also possible to use arraylist, hashtable, map, and hashmap.
Create an array

<Html>
<Head>
<Title> creating an array </title>
</Head>

<Body>
<H1> creating an array <%
Double accounts [];
Accounts = new double [100];
Accounts [3] = 119.63;

Out. println ("account 3 holds $" + accounts [3]);
%>
</Body>
</Html>

Two-dimensional array

<Html>
<Head>
<Title> using multidimensional arrays </title>
</Head>

<Body>
<H1> using multidimen1_arrays <%
Double accounts [] [] = new double [2] [1, 100];

Account [0] [3] = 119.63;
Account [1] [3] = 194.07;

Out. println ("savings account 3 holds $" + accounts [0] [3] + "<br> ");
Out. println ("checking account 3 holds $" + accounts [1] [3]);
%>
</Body>
</Html>

Sorts elements in an array.

<%!
Void doubler (int a [])
    {
For (int I = 0; I <a. length; I ++ ){
A [I] * = 2;
        }
    }
%>

<%
Int array [] = {1, 2, 3, 4, 5 };

Out. println ("before the call to doubler... <br> ");
For (int I = 0; I <array. length; I ++ ){
Out. println ("array [" + I + "] =" + array [I] + "<br> ");
        }

Doubler (array );

Out. println ("after the call to doubler... <br> ");
For (int I = 0; I <array. length; I ++ ){
Out. println ("array [" + I + "] =" + array [I] + "<br> ");
        }
%>

Traverse arrays

While (rs2.next ())
   {
Count ++;
   }
String grade5name [] = new string [count];
Float mark [] = new float [count];
While (rs2.next ())
   {
Grade5name [I] = rs2.getstring ("grade5name ");
Mark [I] = rs2.getfloat ("mark ");
I ++;
   }

Let's take a look at this code. There are two rs. next () judgment loops.
The first while (rs2.next () loop ends and jumps out because rs has been traversed. At this time, the pointer in rs points to the end of the last record, in the second while (rs2.next (), rs2.next () must be false. Of course, the second loop will not be executed. Because the second loop cannot be executed, the corresponding data will never be obtained. If you want to traverse for the second time, you must query it again before the second while loop.
Again, I don't know why you have to use arrays. In fact, it is enough to use a loop instead of an array. It would be better to use a set.

<%
List <string> gradenamelist = new arraylist <string> ();
List <float> marklist = new arraylist <string> ();
While (rs2.next ())
   {
Gradenamelist. add (rs2.getstring ("grade5name "));
Marklist. add (rs2.getfloat ("mark "));
   }
For (int s = 0; s <marklist. size (); s ++)
   {
%>
<Tr>
<Td width = "21"> <input type = "radio" name = "mark" value = "<% = marklist. get (s) %> "/> </td>
<Td width = "909"> <% = gradenamelist. get [s] %> </td>
</Tr>
<%
   }
%>
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.