Core Java (五) Java數組

來源:互聯網
上載者:User
數組的初始化和拷貝

package com.xujin;import java.util.Arrays;public class Test {public static void main(String[] args){int[] a = {2, 3, 5, 7};int[] b = a;//此時a與b引用同一個數組System.out.println(Arrays.toString(a));//[2, 3, 0, 7]System.out.println(Arrays.toString(b));//[2, 3, 0, 7]b = new int[]{12, 13, 14};//用一個匿名數組初始化bSystem.out.println(Arrays.toString(a));//[2, 3, 0, 7]System.out.println(Arrays.toString(b));//[12, 13, 14]//public static int[] copyOf(int[] original, int newLength)int[] c = Arrays.copyOf(a, a.length);c[2] = 100;System.out.println(Arrays.toString(a));//[2, 3, 0, 7]System.out.println(Arrays.toString(c));//[2, 3, 100, 7]//public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)int[] d = {33, 44, 55, 66, 77, 88, 99};System.arraycopy(d, 0, a, 1, 3);System.out.println(Arrays.toString(a));//[2, 33, 44, 55]System.out.println(Arrays.toString(d));//[33, 44, 55, 66, 77, 88, 99]//public static int[] copyOfRange(int[] original, int from, int to)int[] e = Arrays.copyOfRange(a, 1, 3);System.out.println(Arrays.toString(e));//[33, 44]}}

數組的排序

本測試在0~49共50個數裡面任取6個數,按大小排序好了輸出

package com.xujin;import java.util.Arrays;public class Test {static final int MAX = 50;static final int FIT = 6;public static void main(String[] args){int[] total = new int[MAX];int[] result = new int[FIT];for(int i = 0; i < MAX; i++)total[i] = i;for(int i = 0; i < FIT; i++){result[i] = (int) (Math.random() * MAX);}Arrays.sort(result);System.out.println(Arrays.toString(result));}}
數組的二分搜尋,填充,以及判數組是否相同的測試

package com.xujin;import java.util.Arrays;public class Test {public static void main(String[] args){int[] a = new int[100];for(int i = 0; i < a.length; i++)a[i] = i;int[] b = Arrays.copyOfRange(a, 0, 10);System.out.println(Arrays.toString(b));//[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]//public static int binarySearch(int[] a, int key)int r = Arrays.binarySearch(a, 23);System.out.println(r);//23//public static void fill(int[] a, int val)Arrays.fill(b, 8);System.out.println(Arrays.toString(b));//[8, 8, 8, 8, 8, 8, 8, 8, 8, 8]//public static boolean equals(int[] a, int[] a2)int[] c = new int[]{8, 8, 8, 8, 8, 8, 8, 8, 8, 8};if(Arrays.equals(b, c))//此時相等,返回一個trueSystem.out.println("兩個數組大小相等,並且下表相同的元素都對應相等");}}

二維數組初探

package com.xujin;import java.util.Arrays;public class Test {public static void main(String[] args){int[][] matrix = {{1,2,3},{3,2,1},{4,5,6},};System.out.println(matrix[0][2]); //3for(int i = 0; i < matrix.length; i++)System.out.println(Arrays.toString(matrix[i]));//[1, 2, 3]   //[3, 2, 1]   //[4, 5, 6]//快速列印一個二維數組的資料元素列表System.out.println(Arrays.deepToString(matrix));//[[1, 2, 3], [3, 2, 1], [4, 5, 6]]//for each迴圈語句不能自動處理二維數組的每一個元素,他是按照一維數組處理的for(int[] row : matrix)for(int value : row){System.out.println(value * 10);}/*      102030302010405060 */}}

不規則數組

列印出如下數列:


1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5 
1 2 3 4 5 6 
1 2 3 4 5 6 7 
1 2 3 4 5 6 7 8 
1 2 3 4 5 6 7 8 9 
1 2 3 4 5 6 7 8 9 10 

package com.xujin;public class Test {public static void main(String[] args){int[][] test = new int[10][];for(int n = 0; n < 10; n++){test[n] = new int[n + 1];}for(int i = 0; i < 10; i++)for(int j = 0; j < test[i].length; j++){test[i][j] = j + 1;}for(int[] row : test){for(int value : row)System.out.print(value + " ");System.out.println();}}}

上面的程式建立了一個不規則數組,該數組有10個元素,分別是test[0]~test[9],每個元素所包含的int型數不同,test[0]包含1個,test[1]包含2個....test[9]包含10個

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.