创建数组
//
方法一
int intArray[];
intArray = new int [ 100 ];
// 方法二
int intArray[] = new int [ 100 ];
int intArray[];
intArray = new int [ 100 ];
// 方法二
int intArray[] = new int [ 100 ];
访问数组数据项
temp
=
intArray[
3
];
intArray[ 7 ] = 66 ;
intArray[ 7 ] = 66 ;
初始化
//
创建一个数据对象组
autoData[] carArray = autoData[ 100 ];
// 对一个基本类型的数组初始化
int [] intArray = int [ 0 , 23 , 45 , 98 ];
autoData[] carArray = autoData[ 100 ];
// 对一个基本类型的数组初始化
int [] intArray = int [ 0 , 23 , 45 , 98 ];
数组例子 array.java
class
LowArray
{
private long [] a;
public LowArray( int size)
{
a = new long [size];
}
public void setElem( int index, long value)
{
a[index] = value;
}
public long getElem ( int index)
{
return a[index];
}
}
class LowArrayApp
{
public static void main(String [] args)
{
LowArray arr;
arr = new LowArray( 100 );
int nElems = 0 ;
int j;
arr.setElem( 0 , 77 );
arr.setElem( 1 , 99 );
arr.setElem( 2 , 44 );
arr.setElem( 3 , 22 );
arr.setElem( 4 , 55 );
arr.setElem( 5 , 88 );
arr.setElem( 6 , 11 );
arr.setElem( 7 , 00 );
arr.setElem( 8 , 66 );
arr.setElem( 9 , 33 );
nElems = 10 ;
// 打印数组中的数据
for (j = 0 ; j < nElems; j ++ )
System.out.print(arr.getElem(j) + " " );
System.out.println( "" );
// 查找数据
int searchKey = 56 ;
for (j = 0 ; j < nElems; j ++ )
if (arr.getElem(j) == searchKey)
break ;
if (j == nElems)
{
System.out.print( " Can not find " + searchKey);
}
else
System.out.print( " Found " + searchKey);
// 删除数据55
for (j = 0 ;j < nElems; j ++ )
if (arr.getElem(j) == 55 )
break ;
for ( int k = j;k < nElems; k ++ )
arr.setElem(k,arr.getElem(k + 1 ));
nElems -- ;
for (j = 0 ; j < nElems; j ++ )
System.out.print(arr.getElem(j) + " " );
}
}
{
private long [] a;
public LowArray( int size)
{
a = new long [size];
}
public void setElem( int index, long value)
{
a[index] = value;
}
public long getElem ( int index)
{
return a[index];
}
}
class LowArrayApp
{
public static void main(String [] args)
{
LowArray arr;
arr = new LowArray( 100 );
int nElems = 0 ;
int j;
arr.setElem( 0 , 77 );
arr.setElem( 1 , 99 );
arr.setElem( 2 , 44 );
arr.setElem( 3 , 22 );
arr.setElem( 4 , 55 );
arr.setElem( 5 , 88 );
arr.setElem( 6 , 11 );
arr.setElem( 7 , 00 );
arr.setElem( 8 , 66 );
arr.setElem( 9 , 33 );
nElems = 10 ;
// 打印数组中的数据
for (j = 0 ; j < nElems; j ++ )
System.out.print(arr.getElem(j) + " " );
System.out.println( "" );
// 查找数据
int searchKey = 56 ;
for (j = 0 ; j < nElems; j ++ )
if (arr.getElem(j) == searchKey)
break ;
if (j == nElems)
{
System.out.print( " Can not find " + searchKey);
}
else
System.out.print( " Found " + searchKey);
// 删除数据55
for (j = 0 ;j < nElems; j ++ )
if (arr.getElem(j) == 55 )
break ;
for ( int k = j;k < nElems; k ++ )
arr.setElem(k,arr.getElem(k + 1 ));
nElems -- ;
for (j = 0 ; j < nElems; j ++ )
System.out.print(arr.getElem(j) + " " );
}
}