編碼練習——Java-3-數組__Ajax

來源:互聯網
上載者:User

歡迎訪問 部落格新址 建立與輸出數組 數組 - 執行個體 - 求一位元組各元素的和

public class Test {    public static void main(String[] args) {        int[] num = {1,2,3,4,5,6,7,8,9,10};        int sum = 0;        System.out.println("一維數組中各元素的和是:");        for (int i=0; i<num.length; i++) {            if(i==9)                System.out.print(num[i] + "=");            else                System.out.print(num[i] + "+");            sum = sum + num[i];        }        System.out.println(sum);    }}
數組 - 執行個體 - 擷取一維數組的最小值
public class Test {    public static void main(String[] args) {        int[] num = {8,3,4,1,6,10};        System.out.println("輸出一維數組:");        for (int i=0; i<num.length; i++) {            System.out.print(num[i] + "  ");        }        int min = num[0];        for(int j=0; j<num.length-1; j++) {            if (min > num[j+1]) {                min = num[j+1];            }        }        System.out.println("\n一維數組的最小值是:" + min);    }}
二維數組 - 輸出
Java建立數組後,將元素初始化為0.
public class Test {    public static void main(String[] args) {        int a[][] = new int[3][4];        System.out.println("輸出3行4列的數組:");        for (int i=0; i<a.length; i++){            for (int j=0; j<a[i].length; j++) {                System.out.print(a[i][j] + "  ");            }            System.out.println();        }    }}
三維數組 - 輸出
public class Test {    public static void main(String[] args) {        int arr[][][] = new int[][][] {                {{1,2,3},{4,5,6}},                {{7,8,9},{10,11,12}},                {{13,14,15},{16,17,18}}                };        for (int i=0; i<arr.length; i++) {            System.out.println("三維數組的第" + (i+1) + "個元素是一個"+arr[i].length + "維數組,內容如下:");            for(int j=0; j<arr[i].length; j++) {                for (int k=0; k<arr[i][j].length; k++) {                    System.out.print(arr[i][j][k] + "\t");                }                System.out.println();            }        }    }}
執行個體 - 對矩陣進行轉置運算
public class Test {    public static void main(String[] args) {        int arr[][] = new int[][] {{1,2,3},{4,5,6},{7,8,9}};        System.out.println("轉置前的矩陣是:");        printArray(arr);        int arr2[][] = new int[arr.length][arr.length];        for(int i=0; i<arr.length; i++) {            for(int j=0; j<arr[i].length; j++)                arr2[j][i] = arr[i][j];        }        System.out.println("轉置後的矩陣是:");        printArray(arr2);    }    private static void printArray(int[][] arr) {        for (int i=0; i<arr.length; i++) {            for (int j=0; j<arr.length; j++) {                System.out.print(arr[i][j] + "  ");            }            System.out.println();        }    }}
執行個體 - 求方陣的跡
public class Test {    public static void main(String[] args) {        int arr[][] = new int[][] {{1,2,3}, {4,5,6}, {7,8,9}};        int tr = 0;        System.out.println("方陣arr[][]是:");        for (int i=0; i<arr.length; i++) {            for (int j=0; j<arr.length; j++) {                System.out.print(arr[i][j] + "  ");            }            System.out.println();        }        for (int i=0; i<arr.length; i++)            tr += arr[i][i];        System.out.println("方陣arr[][]的跡是:" + tr);    }}
數組的基本操作 數組的基本操作 - 遍曆一維數組
public class Test {    public static void main(String[] args) {        int day[] = new int[] {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};        for (int i=0; i<12; i++) {            System.out.print((i+1) + "月有" + day[i] + "天" + "\t\t");            if ((i+1)%3==0)                System.out.println("\n");        }    }}
數組的基本操作 - 遍曆二維數組
public class Test {    public static void main(String[] args) {        int b[][] = new int[][] {{1}, {2,3}, {4,5,6}};        System.out.println("二維數組是:");        for (int k=0; k<b.length; k++) {            for (int c = 0; c<b[k].length; c++) {                System.out.print(b[k][c] + "  ");            }            System.out.println();        }    }}
數組的基本操作 - 遍曆二維數組(foreach)
public class Test {    public static void main(String[] args) {        int arr2[][] = new int[][] {{3,4,3}, {1,2}};        for (int x[]:arr2) {            for (int e : x) {                System.out.print(e + "  ");            }            System.out.println();        }    }}
數組的基本操作 - 填充數組元素(fill)
import java.util.Arrays;public class Test {    public static void main(String[] args) {        int arr[] = new int[5];        Arrays.fill(arr, 8);        for (int i=0; i<arr.length; i++)            System.out.println("第" + (i+1) + "個元素是:" + arr[i]);    }}
數組的基本操作 - 填充數組元素(fill)
import java.util.Arrays;public class Test {    public static void main(String[] args) {        int arr[] = new int[] {45, 12, 2, 10, 1};        Arrays.fill(arr, 1, 3, 8);        for (int i=0; i<arr.length; i++) {            System.out.println("第" + (i+1) + "個元素是:" + arr[i]);        }    }}
數組的基本操作 - 排序
import java.util.Arrays;public class Test {    public static void main(String[] args) {        int arr[] = new int[] {23, 42, 12, 8, 5, 10};        System.out.println("原一維數組是:");        for (int i=0; i<arr.length; i++) {            System.out.print(arr[i] + "  ");        }        Arrays.sort(arr);        System.out.println("\n升序排列後的數組是:");        for (int i=0; i<arr.length; i++) {            System.out.print(arr[i] + "  ");        }    }}
數組的基本操作 - 複製
import java.util.Arrays;public class Test {    public static void main(String[] args) {        int arr[] = new int[] {23, 42, 12};        System.out.println("複製前的數組是:");        for (int i=0; i<arr.length; i++) {            System.out.print(arr[i] + "  ");        }        int newarr[] = Arrays.copyOf(arr, 5);        System.out.println("\n複製後的數組是:");        for (int i=0; i<newarr.length; i++){            System.out.print(newarr[i] + "  ");        }    }}
數組的基本操作 - 複製
import java.util.Arrays;public class Test {    public static void main(String[] args) {        int arr[] = new int[] {23, 42, 12, 84, 10};        System.out.println("複製前的數組是:");        for (int i=0; i<arr.length; i++) {            System.out.print(arr[i] + "  ");        }        int newarr[] = Arrays.copyOfRange(arr, 0, 3);        System.out.println("\n將數組下標0~3複製到新數組中:");        for (int i=0; i<newarr.length; i++){            System.out.print(newarr[i] + "  ");        }    }}
執行個體 - 對比一維、二維數組所佔記憶體
public class Test {    public static void main(String[] args) {        int num1 = 1024*1024*2;        int[] arr1 = new int[num1];        for (int i=0; i<arr1.length; i++)            arr1[i] = i;        //獲得佔用記憶體總數,並將單位轉換為MB        long memory1 = Runtime.getRuntime().totalMemory()/1024/1024;        System.out.println("用一維數組儲存佔用記憶體總量為:" + memory1);        int num2 = 1024*1024;        int[][] arr2 = new int[num2][2];        for(int i=0; i<arr2.length; i++) {            arr2[i][0] = i;            arr2[i][1] = i;        }        long memory2 = Runtime.getRuntime().totalMemory()/1024/1024;        System.out.println("用二維數組儲存佔用的記憶體總量為:" + memory2);    }}
執行個體 - 使用直接插入排序法排序
public class Test {    public static void main(String[] args) {        int[] array = new int[] {20, 40, 90, 30, 80, 70, 50};        System.out.println("排序前:");        for (int i=0; i<array.length; i++) {            System.out.print(array[i] + "  ");        }        int tmp;        int j;        for (int i=1; i<array.length; i++) {            tmp = array[i];            for (j=i-1; j>=0 && array[j] > tmp; j--) {                array[j+1] = array[j];            }            array[j+1] = tmp;        }        System.out.println("\n排序後:");        for (int i=0; i<array.length; i++) {            System.out.print(array[i] + "  ");        }    }}
執行個體 - 冒泡排序
public class Test {    public static void main(String[] args) {        int[] array = new int[] {63, 4, 24, 1, 3, 13};        System.out.println("冒泡排序法的過程是:");        for (int i=1; i<array.length; i++) {            for (int j=0; j<array.length-i; j++) {                if (array[j] > array[j+1]) {                    int tmp = array[j];                    array[j] = array[j+1];                    array[j+1] = tmp;                }                System.out.print(array[j] + "  ");            }            System.out.print("【");            for (int j=array.length-i; j<array.length; j++) {                System.out.print(array[j] + "  ");            }            System.out.println("】");        }    }}

聯繫我們

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