序
数组是很重要的数据结构,由同一类型相关的数据结构组成是静态实体,有链表,队列,堆栈,数等数据结构,java还提出了类数组的类vector。 这些都是java数据结构的组成部分,正如我们学过的c语言版的数据结构,java数据结构也是来描述数据结构的只是描述语言是java一样而已。
1.数组中最重要的是数组下标,数组下标及数组名是用来给访问者提供访问数组的途径,数据下标从0开始,c[0],就是一个第一个数据第一个元素是c[i-1],数组名的名名规则与变量相同,其访问格式也很简单。
例:c.lenth就是数组的长度。
c[a+b]+=2 就是个数组a+b的值+2,在此数组也有易混淆的地方,那就是数组的第7个元素和数组元素7是两个不相同的概念,初学者一定要区分其区别。
2.空间分配:任何数据都要占用空间,数组也不例外,java中用new来给一个新的数组分配空间。
例:
| int c[ ]=new int[12]; |
| int c[]; c=new int[12]; |
| string b[ ]=new string[100],x[ ]=new string[27]; |
| // 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 ); } } |
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器