数组是很重要的数据结构,由同一类型相关的数据结构组成是静态实体,有链表,队列,堆栈,数等数据结构,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.和其他语言的多维数组基本形式都一样,在次不作分析.
闽公网安备 35060202000074号