深入Java面向对象预备篇(3.研究数组) 序:数组是很重要的数据结构,由同一类型相关的数据结构组成是静态实体,有链表,队列,堆栈,数等数据结构,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]; 他们的初始化值都是0 一个数组可以同时声明多个数组 例:string b[ ]=new String[100],x[ ]=new String[27]; 数组可以声明任何数据类型,double string .. 举个例子来分析: // 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 ); } } 程序中: 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 ); 显示文本框. 由前3个过程你可以看到了数组是怎样建立的了. 3.引用及引用参数:许多编程语言都有通过值的调用 callby value传递参数,当使用调用值时,将产生数值的一个拷贝并传递给被调用的方法. 例如. int hourly Temperatrue[ ]=new int[24]; modify Array(hourlyTemperatrue); 以一个例子说明:// Fig. 7.10: PassArray.java // Passing arrays and individual array elements to methods import java.awt.Container; import javax.swing.*;
public class ArrayPass extends JApplet { JTextArea outputArea; String output;
public void init() { outputArea = new JTextArea(); Container c = getContentPane(); c.add( outputArea );
int a[] = { 1, 2, 3, 4, 5 }; output = "Effects of passing entire " + "array call-by-reference:\n" + "The values of the original array are:\n";
for ( int i = 0; i < a.length; i++ ) output += " " + a[ i ]; modifyArray( a ); // array a passed call-by-reference output += "\n\nThe values of the modified array are:\n";
for ( int i = 0; i < a.length; i++ ) output += " " + a[ i ]; output += "\n\nEffects of passing array " + "element call-by-value:\n" + "a[3] before modifyElement: " + a[ 3 ]; modifyElement( a[ 3 ] ); output += "\na[3] after modifyElement: " + a[ 3 ]; outputArea.setText( output ); } public void modifyArray( int b[] ) { for ( int j = 0; j < b.length; j++ ) b[ j ] *= 2; } public void modifyElement( int e ) { e *= 2; } } 分析1. outputArea = new JTextArea(); Container c = getContentPane(); c.add( outputArea ); 定义一个container用来输出结果. 2. int a[] = { 1, 2, 3, 4, 5 };定义数组a[] 3. output = "Effects of passing entire " + "array call-by-reference:\n" + "The values of the original array are:\n"; 在output字符串后追加标记
for ( int i = 0; i < a.length; i++ ) output += " " + a[ i ]; 在TextArea上输出a[i]为1,2,3,4,5 4. modifyArray( a ); // array a passed call-by-reference 引用参数 output += "\n\nThe values of the modified array are:\n"; 追加字符 for ( int i = 0; i < a.length; i++ ) output += " " + a[ i ]; 循环显示 output += "\n\nEffects of passing array " + "element call-by-value:\n" + "a[3] before modifyElement: " + a[ 3 ] 与返回类 结合 public void modifyArray( int b[] ) { for ( int j = 0; j < b.length; j++ ) b[ j ] *= 2; } 返回了计算后的a[]=b[j]*2 b[j]是临时的 此处体现callby value 数组b是一个拷贝,计算结束后传递给被调用的方法,b在计算后自动销毁. 输出的结果是a[3] brefore modifyelment:8 5.modifyElement( a[ 3 ] ); output += "\na[3] after modifyElement: " + a[ 3 ]; outputArea.setText( output );与 过程结合 public void modifyElement( int e ) { e *= 2; } 返回了计算后的a[3]after modifyelment:8 e是临时变量,运行后自动销毁. 5. 数组排序:排序使程序按某种顺序进行排列,升序或降序.这是计算机数据处理中中应用最多的一项.例如银行的帐户排序,工资排序等.在次不作过多介绍 6.数组查找:分线性插找和折半查找:对于小型的数组线性查找效果可能很好,对于大型数据效率就很低了,可以用折半查找,效率会很大的提高, 折半查找的方法是,例如查找含63个元素只需要比较6次大小 第一次查找下标(1+63)/2=32查找 若大于关键子,则到下标为1-32中查找,若小与关键子,则到33-63中查找,若找不到,继续到下一个范围中找,就是在将32分半,以上面的方法以此类推,只道找到为止. 而查找的最多比较次数:满足2的k次方大于n的k次的最小整数值.由此可推算对于10亿个数组来说用线性查找5亿次和用折半查找30次是多么大的差距. 7.和其他语言的多维数组基本形式都一样,在次不作分析.
上图左边为第一个程序执行的结果,右边为第二个图执行结果

以上就是数组的一些基本概念.和应用,加上前2长文章,你已经对与面向对象的基本概念有了一定的了解了.

|