java 數組操作,java數組操作

來源:互聯網
上載者:User

java 數組操作,java數組操作

數組是一種資料結構,用來儲存同一類型值的集合。

數組作為對象允許使用new關鍵字進行記憶體配置,在使用數組之前,必須首先定義陣列變數所屬的類型。

數組通過一個整型下標可以訪問數組中得每一個值,例如,如果存在數組a。則a[i]就是數組中下標為i的整數(下標從0開始)。

1.數組的聲明及初始化

 1      //聲明 2         int [] iArr ; 3         String sArr []; 4          5         //聲明並分配大小 6         int [] iArr2 = new int[3]; //數組內預設值為0 7         String [] sArr2 = new String[10];//數組預設值為null 8         System.out.println(Arrays.toString(iArr2));//列印數組   9         System.out.println(Arrays.toString(sArr2));10         /*輸出11          * [0, 0, 0]12            [null, null, null, null, null, null, null, null, null, null]13          */14         15         //聲明並賦值16         int [] iArr3 =  new int[]{2,3,4,6};17         String [] sArr3 = {"ONE","TWO","THREE"};18         System.out.println(Arrays.toString(iArr3));//[2, 3, 4, 6]19         System.out.println(Arrays.toString(sArr3));//[ONE, TWO, THREE]20         21         //簡單使用22         iArr = new int[]{1,2,3};23         sArr = new String[2];24         sArr[0] = "hello";25         sArr[1] = "friend";26         System.out.println(iArr[1]);//227         System.out.println(Arrays.toString(sArr));//[hello, friend]

2.數組的操作

(1)數組的遍曆

    //數組的遍曆        int[] arr = {2,4,5,9,7};         for(int i = 0; i < arr.length; i++){            System.out.print(arr[i]+"\t");//2    4    5    9    7        }        System.out.println();        for(int num : arr){            System.out.print(num+"\t");//2    4    5    9    7        }

(2)數組的填充

Arrays.fill()方法能夠對數組進行填充,fill()方法有很多重載的方法,能夠滿足任意數組元素的填充或者替換。

     int[] arr = new int[5];        //填充        Arrays.fill(arr, 6);        System.out.println(Arrays.toString(arr));//[6, 6, 6, 6, 6]                //替換        Arrays.fill(arr,2,4,0);        System.out.println(Arrays.toString(arr));//[6, 6, 0, 0, 6]

(3)數組的排序

Arrays.sort()方法能夠對數組進行排序

     int[] arr = {5, 4, 3, 2, 1};        Arrays.sort(arr);        System.out.println(Arrays.toString(arr));//[1, 2, 3, 4, 5]

(4)數組的複製

Arrays類中copyOf()方法和copyOfRange()方法可以實現對數組的複製,

前者是複製數組到指定長度,後者是將指定長度的數組複製到一個新的數組中(或者覆蓋之前的數組)。

coypOf(arr, int newLength) 可用於數組的擴容

-arr: 要進行複製的數組

-newLength: 複製後數組的長度,如果新數組複製的長度大於arr,則用0或者null補充。如果新數組小於arr的長度,則複製arr的0為開始直至滿足數組的長度。

int[] arr = new int[]{3,4,5};arr = Arrays.copyOf(arr, arr.length*2);System.out.println(Arrays.toString(arr));//[3, 4, 5, 0, 0, 0]int[] newArr = Arrays.copyOf(arr, 2);System.out.println(Arrays.toString(newArr));//[3, 4]

copyOfRange(arr, int fromIndex, int toIndex)

-arr: 要複製的數組

-fromIndex:指定要複製數組的開始索引位置,不能大於arr的長度

-toIndex:指定要複製數組的最後索引位置(不包括toIndex) 可以大於arr的長度

   int[] arr = {5, 7, 8, 2, 9};    arr = Arrays.copyOfRange(arr, 0, arr.length*2);    System.out.println(Arrays.toString(arr));//[5, 7, 8, 2, 9, 0, 0, 0, 0, 0]    int[] newArr = Arrays.copyOfRange(arr, 1, 3);    System.out.println(Arrays.toString(newArr));//[7, 8]

在System類中的arraycopy方法將數組的元素copy到另一個數組中,方法為:

System.arraycopy(src, srcPos, dest, destPos, length)

-src: 要複製的數組

-srcPos:將要複製的數組的索引位置

-dest: 目標數組

-destPos:目標數組的索引位置

-length:要複製的長度,從索引位置開始計數

int[] arr = {6, 4, 11, 12};int [] newArr = new int[]{5,4,9,3,4,2};System.arraycopy(arr, 2, newArr, 0, 2);
System.out.println(Arrays.toString(newArr));//[11, 12, 9, 3, 4, 2]

 3.數組的排序

(1) 冒泡排序

冒泡的基本思路是對比相鄰的元素值,如果滿足條件就交換元素值,把較小的元素移動到數組前面,把大的元素移動到數組後面。

        //冒泡排序        int[] arr = new int[]{12, 3, 65, 45, 88, 1, 7};
    for(int i = 0; i < arr.length -1; i++){ for(int j = 0; j < arr.length - 1 - i; j++){ if(arr[j] > arr[j+1]){ int temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } } System.out.println(Arrays.toString(arr));//[1, 3, 7, 12, 45, 65, 88]

(2)直接選擇排序

直接選擇排序的思路是將指定排序位置與其他數組元素分別對比,如果滿足條件就交換元素,找出最大數後與“最後”一個元素交換。

        //直接選擇排序        int[] arr = new int[]{12, 3, 65, 45, 88, 1, 7};        int index;        for(int i = 1; i < arr.length; i++){            index =0;            //找出最大值            for(int j = 0; j <= arr.length - i; j++){                if(arr[j] > arr[index]){                    index = j;                }            }            //最大值放到最後的位置上            int temp = arr[index];            arr[index] = arr[arr.length - i];            arr[arr.length - i] = temp;        }        System.out.println(Arrays.toString(arr));//[1, 3, 7, 12, 45, 65, 88]

(3)反轉排序

將數組的元素反轉替換,即將元素的最後一個元素與第一個元素進行交換,倒數第二個元素與數組的第二個元素進行交換。。。。

        //翻轉排序 -將數組的位置反過來        int[] arr = new int[]{1, 2, 3, 4, 5, 6, 7};        int arrLength = arr.length;        for(int i = 0; i < arrLength/2 ; i++ ){            int temp = arr[i];            arr[i] = arr[arrLength-1-i];            arr[arrLength-1-i] = temp;        }        System.out.println(Arrays.toString(arr));//[7, 6, 5, 4, 3, 2, 1]

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.