Java/JSP/Array

Материал из Java эксперт
Версия от 07:10, 1 июня 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Creating an Array

<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>





Demo for loop

<%--
  Copyright (c) 2002 by Phil Hanna
  All rights reserved.
  
  You may study, use, modify, and distribute this
  software for any purpose provided that this
  copyright notice appears in all copies.
  
  This software is provided without warranty
  either expressed or implied.
--%>
<%!
   static final String[] COLORS = {
      "#CA9A26",
      "#3BF428",
      "#F7E339",
      "#FF40FF",
   };
%>
<%
   for (int i = 0; i < COLORS.length; i++) {
      String color = COLORS[i];
%>
<div style="background-color: <%= color%>;
            font-size: 12pt;
            font-weight: bold;">
   This is color <%= color %>
</div>
<% } %>





Int array

<HTML>
  <HEAD>
    <TITLE>My First JSP</TITLE>
  </HEAD>
  <BODY>
    <H1>Using the if Statement</H1>
    <%
        int array[] = {1, 2, 3, 4, 5}, sum = 0;
        for (int i = 0;
            i < array.length;
            sum += array[i++]);
        out.println("The average = " + sum / array.length);
    %>
  </BODY>
</HTML>





Using Multidimensional Arrays

<HTML>
  <HEAD>
    <TITLE>Using Multidimensional Arrays</TITLE>
  </HEAD>
  <BODY>
    <H1>Using Multidimensional Arrays</H1>
    <%
        double accounts[][] = new double[2][100];
        accounts[0][3] = 11.09;
        accounts[1][3] = 19.07;
        out.println("Savings Account 3 holds $" + accounts[0][3] + "<BR>");
        out.println("Checking Account 3 holds $" + accounts[1][3]);
     %>
  </BODY>
</HTML>