当前页面: 开发资料首页 → Java 专题 → 数组在Java编程中的应用
数组在Java编程中的应用
摘要: 数组是很重要的数据结构,由同一类型相关的数据结构组成是静态实体
<table cellSpacing=0 cellPadding=5 width=570 bgColor=#fbe392 border=0>
<tr>
<td>
<table cellSpacing=0 cellPadding=5 width=570 border=0>
<tr>
<td align=middle width=200 bgColor=#e1b004>天极IT资讯短信服务 电脑小技巧
<table cellSpacing=0 cellPadding=3 width="100%" bgColor=#fffcc0 border=0>
function check4() { if (dn.mobile.value.length!=11) { alert("手机号码不正确!"); dn.mobile.focus(); return false; } return true; }
<form name=dn onsubmit="return check4()" action=http://www.my5757.com/tj/join.jsp target=_blank>
<tr vAlign=center>
<td>资费:包月5元
手机: <input style="BORDER-RIGHT: #000000 1px solid; BORDER-TOP: #000000 1px solid; BORDER-LEFT: #000000 1px solid; BORDER-BOTTOM: #000000 1px solid; HEIGHT: 16px" size=11 name=mobile> <input type=image height=18 width=45 src="http://www.my5757.com/yesky/images/d34.gif" align=middle border=0 name=image2> </td></tr><input type=hidden value=yjq name=stype> </form></table></td>
<td width=370 bgColor=#fbc403>介绍:细处着手,巧处用功。高手和菜鸟之间的差别就是:高手什么都知道,菜鸟知道一些。电脑小技巧收集最新奇招高招,让你轻松踏上高手之路。 </td></tr></table></td></tr></table>
序
数组是很重要的数据结构,由同一类型相关的数据结构组成是静态实体,有链表,队列,堆栈,数等数据结构,java还提出了类数组的类vector。这些都是java数据结构的组成部分,正如我们学过的c语言版的数据结构,java数据结构也是来描述数据结构的只是描述语言是java一样而已。
<iframe align=right marginWidth=0 marginHeight=0 src="http://images.chinabyte.com/adjs/iframe-pip/y-software-pip.html" frameBorder=0 width=360 scrolling=no height=300></iframe> 1.数组中最重要的是数组下标,数组下标及数组名是用来给访问者提供访问数组的途径,数据下标从0开始,c[0],就是一个第一个数据第一个元素是c[i-1],数组名的名名规则与变量相同,其访问格式也很简单。
例:c.lenth就是数组的长度。
c[a+b]+=2 就是个数组a+b的值+2,在此数组也有易混淆的地方,那就是数组的第7个元素和数组元素7是两个不相同的概念,初学者一定要区分其区别。
2.空间分配:任何数据都要占用空间,数组也不例外,java中用new来给一个新的数组分配空间。
例:
<table width="100%" bgColor=#ffffff>
<tr>
<td>int c[ ]=new int[12]; </td></tr></table>
其格式等同于
<table width="100%" bgColor=#ffffff>
<tr>
<td>int c[];
c=new int[12];</td></tr></table>
他们的初始化值都是0。
一个数组可以同时声明多个数组:
例:
<table width="100%" bgColor=#ffffff>
<tr>
<td>string b[ ]=new String[100],x[ ]=new String[27];</td></tr></table>
数组可以声明任何数据类型,double string ..
举个例子来分析:
<table width="100%" bgColor=#ffffff>
<tr>
<td>// Fig. 7.5: InitArray.java
// initialize array n to the even integers from 2 to 20
import javax.swing.*;
public class InitArray {
public static void main( String args[] )
{
final int ARRAY_SIZE = 10;
int n[]; // reference to int array
String output = "";
n = new int[ ARRAY_SIZE ]; // allocate array
// Set the values in the array
for ( int i = 0; i < n.length; i++ )
n[ i ] = 2 + 2 * i;
output += "Subscript\tValue\n";
for ( int i = 0; i < n.length; i++ )
output += i + "\t" + n[ i ] + "\n";
JTextArea outputArea = new JTextArea( 11, 10 );
outputArea.setText( output );
JOptionPane.showMessageDialog( null, outputArea,
"Initializing to Even Numbers from 2 to 20",
JOptionPane.INFORMATION_MESSAGE );
System.exit( 0 );
}
}</td></tr></table>
程序中:
1.final int ARRAY_SIZE=10限定词final声明常数变量ARRAY_SIZE其值是10。
2. n = new int[ ARRAY_SIZE ]声明了n数组其长度不能超过10
3.for ( int i = 0; i < n.length; i++ ) n[ i ] = 2 + 2 * i; 指定了程序的方法即输出10个从2开始的偶数.其下标分别计为0-9的10个数。
4.output += "Subscript\tValue\n";
for ( int i = 0; i < n.length; i++ )
output += i + "\t" + n[ i ] + "\n"; 在output后面追加字符串.显示数组下标即计算结果.
5 JTextArea outputArea = new JTextArea( 11, 10 );
outputArea.setText( output );
创建一个新的文本框,把output放入其中.
JOptionPane.showMessageDialog( null, outputArea,"Initializing to Even Numbers from 2 to 20",JOptionPane.INFORMATION_MESSAGE );
zmbbs=1;
↑返回目录
前一篇:
Struts快速学习指南之一
后一篇:
J2EE Web服务客户端质量报告(二)