Jquery中文网 www.jquerycn.cn
Jquery中文网 >  脚本编程  >  java  >  正文 jsp数组定义,遍历输出方法

jsp数组定义,遍历输出方法

发布时间:2017-12-13   编辑:www.jquerycn.cn
jquery中文网为您提供jsp数组定义,遍历输出方法等资源,欢迎您收藏本站,我们将为您提供最新的jsp数组定义,遍历输出方法资源

数组定义对于开始不知道长度的数组,可以用vector,还可能arraylist、hashtable、map、hashmap
简单创建一个数组

<blockquote>

<html>
  <head>
    <title>creating an array</title>
  </head>

  <body>
    <h1>creating an array</h1>
    <%
        double accounts[];
        accounts = new double[100];
        accounts[3] = 119.63;

        out.println("account 3 holds $" accounts[3]);
     %>
  </body>
</html>

</blockquote>

二维数组

<blockquote>

<html>
  <head>
    <title>using multidimensional arrays</title>
  </head>

  <body>
    <h1>using multidimensional arrays</h1>
    <%
        double accounts[][] = new double[2][100];

        accounts[0][3] = 119.63;
        accounts[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>

</blockquote>

对数组中元素排序

<blockquote>

<%!
    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>");
        }
    %>

</blockquote>

遍历数组

<blockquote>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 ;
   }</blockquote>

自己看一下这段代码,有两个rs.next()的判断循环遍历。
第一个while(rs2.next()),循环之所以会结束,跳出,是因为rs已经遍历完了,这个时候, rs里面的指针是指向最后一条记录的后面的,所以,在第二个while(rs2.next())的时候,rs2.next()肯定是false了,当然不会再执行第二个循环。因为第二个循环不可能会执行,所以,永远都不会得到相应的数据的呀。如果你想要第二次遍历,那必须在第二个while循环之前再查询一次才可以。
再有,不知道你为什么非要用数组,其实,用一个循环就够了,不要用数组,用集合会好一些。

<blockquote><%
   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>
<%
   }
%>
</blockquote>

您可能感兴趣的文章:
php遍历数组之list foreach each用法总结
php遍历数组的几种方法(for foreach list each while)
php遍历数组list foreach each方法实例
js数组声明与数组遍历的方法
php数组遍历方法详解(for foreach list each key)
php遍历数组 list foreach each方法总结
jsp自定义标签ifelse与遍历自定义标签
servlet与jsp基础教程(11)-JSP及语法概要
php无限遍历目录代码
php学习笔记之数组遍历的代码举例

[关闭]