第一階段——javaSE——03.java數組

數組是用來存儲固定大小同類型元素。

  1. Java 數組的聲明、創建和初始化

聲明數組首先必須聲明數組變量,才能在程序中使用數組。下面是聲明數組變量的語法:

dataType[] arrayRefVar; // 首選的方法 或

dataType arrayRefVar[]; // 效果相同,但不是首選方法

創建數組

arrayRefVar = new dataType[arraySize];

聲明創建

dataType[] arrayRefVar = new dataType[arraySize];

數組的元素是通過索引訪問的。數組索引從 0 開始,所以索引值從 0 到 arrayRefVar.length-1。

初始化:可分爲靜態初始化和動態初始化

1)靜態初始化的簡單寫法

int[] arr2 = {1,2,3,4,5};//聲明、創建、初始化

2)動態初始化

int[] arr1 = new int[5]; //聲明、創建

arr1[0] = 0;//初始化

arr1[0] = 1;

arr1[0] = 2;

arr1[0] = 3;

arr1[0] = 4;

2.引用對象的內存分析

public class Test1 {

    public static void main(String[] args) {

        int[] x = null;

        x = new int[3];

        x[0] = 10;

        x[1] = 20;

        x[2] = 30;

        x = null;

    }

}

 

3.處理數組

(1)調用

數組的訪問通過索引完成,即:“數組名稱[索引]”,如arr2 [2], 但是需要注意的是,數組的索引從0開始,所以索引的範圍就是0 ~ 數組長度-1,例如開闢了3個空間的數組,所以可以使用的索引是:0,1,2,如果此時訪問的時候超過了數組的索引範圍,會產生java.lang.ArrayIndexOutOfBoundsException 異常信息;

(2)遍歷
第一種

int data[] = new int[3]; /*開闢了一個長度爲3的數組*/
data[0] = 10; // 第一個元素
data[1] = 20; // 第二個元素
data[2] = 30; // 第三個元素
for(int x = 0; x < data.length; x++) {
    System.out.println(data[x]); //通過循環控制索引
 }


第二種for-each(增強for)

int [] array = {1,2,3};

for(int i : array){

    System.out.println(i);

}

(3)排序

java.util.Arrays.sort(arrayName) ;
import java.util.Arrays;

public class ArrayUtil {

    public static void main(String[] args) {

        int[] a = new int[]{1, 33, 5, 44, 76};//1,5,33,44,76

        System.out.println("排序之前:");

        arrayPrint(a);//1,5,33,44,76

        Arrays.sort(a);

        System.out.println("排序之後:");

        //改變原數組

        arrayPrint(a);//1,5,33,44,76

    }



    public static void arrayPrint(int[] c) {

        if (c == null) {

            return;

        }

        for (int j = 0; j < c.length; j++) {

            System.out.print(c[j] + " ");

        }

        System.out.println();

    }

}

 

(4)拷貝
將指定源數組中的數組從指定位置複製到目標數組的指定位置。j

ava.lang.System.arraycopy(Object src,int srcPos,Object dest, int destPos,int length);
public class ArrayUtil {

    //拷貝

    public static void main(String[] args) {

        int[] src = new int[]{1, 2, 3, 4, 5, 6, 7, 8};

        int[] dest = new int[3];

        arrayPrint(src);//1,2,3,4,5,6,7,8

        arrayPrint(dest);0,0,0

        //方法一

        // for(int i=2;i<5;i++){

        // dest[i-2]=src[i];

        // }

        //方法二

        System.arraycopy(src, 2, dest, 0, 3);

        arrayPrint(src);//1,2,3,4,5,6,7,8

        arrayPrint(dest);3,4,5

    }



    public static void arrayPrint(int[] c) {

        if (c == null) {

            return;

        }

        for (int j = 0; j < c.length; j++) {

            System.out.print(c[j] + " ");

        }

        System.out.println();

    }

}

方法二:複製指定的數組,用零截取或填充(如有必要),以便複製具有指定的長度。

java.util.Arrays.copyOf(源數組名稱,新數組長度)

import java.util.Arrays;



public class ArrayUtil {

    public static void main(String[] args) {

        int[] src = new int[]{1, 2, 3, 4, 5, 6, 7, 8};

        arrayPrint(src);//1,2,3,4,5,6,7,8



        int[] dest1 = Arrays.copyOf(src, 7);

        //Arrays.copyOf不改變原數組

        arrayPrint(src);//1,2,3,4,5,6,7,8

        arrayPrint(dest1);//1,2,3,4,5,6,7,



        int[] dest2 = Arrays.copyOf(src, 10);

        arrayPrint(dest2);//1,2,3,4,5,6,7,8,0,0



    }



    public static void arrayPrint(int[] c) {

        if (c == null) {

            return;

        }

        for (int j = 0; j < c.length; j++) {

            System.out.print(c[j] + " ");

        }

        System.out.println();

    }

}

 

(5)數組轉集合

java.util.Arrays.asList(arg);
String[] array = new String[] {"zhu", "wen", "tao"};

// String數組轉List集合

List<String> mlist = Arrays.asList(array);

// 輸出List集合

for (int i = 0; i < mlist.size(); i++) {

   System.out.println("mlist-->" + mlist.get(i));

}

 

注意:數組int[]用Integer[],double[]用Double[] ,因爲List集合是對象的集合,而int、double等不是對象,所以需要用字段的對應對象類

可以使用apache commons-lang3  jar 

org.apache.commons.lang3.ArrayUtils.toObject(int[]) 方法 先轉成Integer數組

再使用 java.util.Arrays.asList(Integer...) 轉成list 

 public static void main(String[] args){

        int[] src = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

        Integer[] is = ArrayUtils.toObject(src);

        List<Integer> asList = Arrays.asList(is);

        System.out.println(asList);

    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章