JAVA —— 数组
import java.util.Arrays;
public class Array {
public static void main(String[] args){
Array test=new Array();
test.test1();
test.test2();
test.test3();
test.test4();
}
//test1
public void test1(){
System.out.println("*****************************");
System.out.println("test1_initial");
System.out.println("*****************************");
//array1
int[] array1;
array1=new int[5];
int length=array1.length;
for(int i=0;i<length;i++){
System.out.println("int[] "+array1[i]);
}
//array2
float array2[]=new float[5];
for(int i=0;i<array2.length;i++){
System.out.println("float[] "+array1[i]);
}
//array3
boolean array3[]={true,false,false,false,false};
for(boolean element:array3){
System.out.println("boolean[] "+element);
}
//array4 数组长度不可变,长度可以为0
double array4[]=new double[0];
}
//test2
public void test2(){
System.out.println("*****************************");
System.out.println("test2_copy");
System.out.println("*****************************");
//array5
short[] array5={1111,2222,3333,4444,5555};
for(short element:array5){
System.out.println("array5[] "+element);
}
//array6和array5指向同一数组
short array6[]=array5;
array6[0]=6666;
System.out.println("array5[0] & array6[0] is "
+array5[0]+"&"+array6[0]);
//array7, 类Arrays包含对数组
//进行操作的一些列方法(如搜索、排序)
short[] array7=Arrays.copyOf(array5,array5.length-1);
for(short element:array7){
System.out.println("array7[] "+element);
}
array7[0]=0;
System.out.println("array5[0] & array7[0] is "
+array5[0]+"&"+array7[0]);
}
//test3
public void test3(){
System.out.println("*****************************");
System.out.println("test2_search & sort");
System.out.println("*****************************");
//array8,sort()
long[] array8={100000,100005,100008,100006,100001,
100003,100010,100011,100020,100099};
Arrays.sort(array8);
System.out.println("aftter sorting array[8] "+
Arrays.toString(array8));
//array9, binarySearch()
byte[] array9={1,2,25,54,89,123,68,23,45,98,100};
Arrays.sort(array9);//
int searchIndex=Arrays.binarySearch(array9,(byte)23);
System.out.println("searchIndex array9 "+searchIndex);
}
//test4
public void test4(){
System.out.println("*****************************");
System.out.println("test4_multidimensional_array");
System.out.println("*****************************");
//array10,initial
int[][] array10={
{12,23,54,66},
{456,8798,1245,7855,456},
{77,89,653,123,1111,2222},
{852,963,741,753,7777,8888,99999}
};
System.out.println("行数 array10.length is "+array10.length);
for(int i=0;i<array10.length;i++){
System.out.println("line "+i+" has "
+array10[i].length+" column");
}
}
}