Java数组

Java中一维数组的声明方式:

type[] var  

或者

type var[]

使用关键字new创建数组对象(数组长度为n)

var = new type[n];

var = {item0, item1, ... item(n-1)}

var = new type[]{item0, item1, ... item(n-1)}

 

动态初始化:数组定义与为数组分配空间和赋值操作分开;

type[] var;

var = new type[n];

var[0] = xx;

var[1] = xx;

...

var[n - 1] = xx;

 

静态初始化:在定义数字的同时就为数组元素分配空间并赋值

type[] var = {item0, item1, ... itemn}

type[] var = new type[]{item0, item1, ... itemn}

 

默认初始化:创建数组并给数组分配空间,每个元素按照变量的规则被隐士初始化。

String strArray4[] = new String[5];

int asciia = 97;
char c;
int i;
for (i = 0; i < 5; i++) {
    c = (char) (asciia + i);
    strArray4[i] = String.valueOf(c);
}

 

创建数组

        String[] strArray1 = new String[5];
        String[] strArray2 = {"a", "b", "c", "d", "e"};
        String[] strArray3 = new String[]{"a", "b", "c", "d", "e"};
        String strArray4[] = new String[5];
        String strArray5[] = {"a", "b", "c", "d", "e"};
        String strArray6[] = new String[]{"a", "b", "c", "d", "e"};

        int asciia = 97;
        char c;
        int i;
        for (i = 0; i < 5; i++) {
            c = (char) (asciia + i);
            strArray1[i] = String.valueOf(c);
            strArray4[i] = String.valueOf(c);
        }
View Code

 

打印数组

Log.v("Array", strArray1.toString());
// java.lang.String;@38bd5147

Log.v("Array", Arrays.toString(strArray1));
// [a, b, c, d, e]
View Code

 

检索是否包含特定值

        String target = "a";
        boolean isContained = Arrays.asList(strArray1).contains(target);
        Log.v("Array", "isContained: " + isContained);

        boolean hasItem = false;
        for (i = 0; i < 5; i++) {
            if (target == strArray1[i]) {
                hasItem = true;
            }
        }
        Log.v("Array", "hasItem: " + hasItem);
View Code

 

数组合并

        String[] strArray7 = Arrays.copyOf(strArray1, strArray1.length + strArray2.length);
        System.arraycopy(strArray2, 0, strArray7, strArray1.length, strArray2.length);
        Log.v("Array", Arrays.toString(strArray7));

        String[] strArray8 = new String[10];
        System.arraycopy(strArray1, 0, strArray8, 0, strArray1.length);
        System.arraycopy(strArray2, 0, strArray8, strArray1.length, strArray2.length);
        Log.v("Array", Arrays.toString(strArray8));
View Code

 

String/Array 转换

        // string to array
        String str1 = "a,b,c,d,e";
        String[] strArray9 = str1.split(",");
        Log.v("Array", Arrays.toString(strArray9));

        // array to string
        StringBuilder builder = new StringBuilder();
        for (String s : strArray1) {
            builder.append(s);
        }
        Log.v("Array", builder.toString());
View Code

 

二维数组

        int[][] intArray1 = new int[5][];
        int[][] intArray2 = new int[5][5];
        int[][] intArray3 = {{1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10}};
        int intArray4[][] = new int[5][];

 

三维数组:

        int[][][] three1 = new int[5][][];
        int[][][] three2 = new int[5][2][];
        int[][][] three3 = new int[5][2][1];
        int[][][] three4 = {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}};

 

posted on 2016-07-19 16:42  alvin.zhang  阅读(171)  评论(0编辑  收藏  举报

导航