1. 程式人生 > >三、java數組

三、java數組

求最大值 算法 pub 工具 bsp test ole arrays 引用

1、一維數組

public class Testarray {
    public static void main(String[] args) {
        int t = 1;
        //數組的定義(二者缺一不可):聲明(數據類型、變量名[])、分配空間
        int arr[] = new int[4];
        int arr2[];
        arr2 = new int[3];
        int[] arr3 = new int[5];
        int[] arr4;
        arr4 = new
int[6]; String strs[] = new String[3]; //數組元素訪問時,數組的下標從零開始 //為數組元素賦值 arr[0] = 2; arr[1] = 5; arr[2] = 6; arr[3] = 1; strs[0] = "aaa"; strs[1] = "eee"; strs[2] = "www"; System.out
.println(arr[0]); System.out.println(strs[2]); //數組元素的長度,一旦分配空間後,數組長度不可變 System.out.println(arr.length); //數組元素的遍歷 System.out.println(arr[0]); for(int i = 0; i < arr.length; i++) { System.out.println(arr[i]); }
//數組默認值(或者叫初始化值,在聲明和分配空間後,不賦值) //對於byte、short、int、long而言,默認值為0 long[] a1 = new long[5]; for(int i = 0; i < a1.length; i++) { System.out.println(a1[i]); } //對於float、double而言,默認值是0.0 double[] a2 = new double[6]; for(int i = 0; i < a2.length; i++) { System.out.println(a2[i]); } //對於char而言,默認值是空格 char[] a3 = new char[5]; for(int i = 0; i < a3.length; i++) { System.out.println(a3[i]); } //對於boolean而言,默認值是false boolean[] a4 = new boolean[3]; for(int i = 0; i < a4.length; i++) { System.out.println(a4[i]); } //引用數據類型,默認值是null String[] a5 = new String[6]; for(int i = 0; i < a5.length; i++) { System.out.println(a5[i]); } //定義數組的其他方式 int arr6[] = new int[] {3,6,7,2,1}; int arr7[] = {3,6,7,2,1}; String arr8[] = new String[] {"a","gg","ww"}; arr6[0] = 50; System.out.println(arr6.length); System.out.println(arr6[0]); for(int i = 0; i < arr6.length; i++) { System.out.println(arr6[i]); } //練習 String[] arr9 = new String[]{"Mike","jimmy","tommy","jerry"}; for(int i = 0; i < arr9.length; i++) { System.out.println(arr9[i]); } int a[] = new int[100]; for(int i = 0; i < a.length; i++) { a[i] = (i + 1) * 2; System.out.println(a[i]); } } }

2、二維數組

public class Testarray2 {
    public static void main(String[] args) {
        //定義
        int arr[][] = new int[3][2];
        int[] arr2[] = new int[5][2];
        int[][] arr3 = new int[3][5];
        //賦值
        arr[0][0] = 2;
        arr[0][1] = 5;
        arr[1][0] = 2;
        arr[1][1] = 6;
        arr[2][0] = 1;
        arr[2][1] = 5;
        //數組元素長度
        System.out.println(arr.length);//行數
        System.out.println(arr[0].length);//第0行的列數
        //遍歷
        for(int i = 0; i < arr.length; i++) {
            for(int j = 0; j < arr[i].length; j++) {
                System.out.print(arr[i][j] + " ");
            }
            System.out.println();
        }
        
        //定義數組其他方式
        int arr4[][] = new int[3][];
        arr4[0] = new int[4];
        arr4[1] = new int[3];
        arr4[2] = new int[5];
        
        System.out.println(arr4.length);
        System.out.println(arr4[2].length);
        for(int i = 0; i < arr4.length; i++) {
            for(int j = 0; j < arr4[i].length; j++) {
                System.out.print(arr4[i][j] + " ");
            }
            System.out.println();
        }
        
        
        int[][] arr5 = new int[][] {{3,7},{2,5,8},{2,6,9,5}};
        int[][] arr6 = {{3,7},{2,5,8},{2,6,9,5}};
        for(int i = 0; i < arr6.length; i++) {
            for(int j = 0; j < arr6[i].length; j++) {
                System.out.print(arr6[i][j] + " ");
            }
            System.out.println();
        }
    }
}

3、算法

public class Testarray3 {
    public static void main(String[] args) {
        int[] arr = new int[] {12,43,9,0,-65,-99,100,9};
        //求最大值
        int max = arr[0];
        for(int i = 1; i < arr.length; i++) {
            if(max < arr[i]) {
                max = arr[i];
            }
        }
        System.out.println(max);
        
        //求最小值
        int min = arr[0];
        for(int i = 1; i < arr.length; i++) {
            if(min > arr[i]) {
                min = arr[i];
            }
        }
        System.out.println(min);
        
        //求和
        int sum = 0;
        for(int i = 0; i < arr.length; i++) {
            sum = sum + arr[i];
        }
        System.out.println(sum);
        
        //交換兩個數的值
        int x = 5;
        int y = 8;
        int a;
        a = x;
        x = y;
        y = a;
        System.out.println(x);
        System.out.println(y);    
    }
}

4、冒泡排序

import java.util.Arrays;

public class Testarray4 {
    public static void main(String[] args) {
        //冒泡排序
        int[] arr = new int[] {6,5,4,3,2};
        
        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;
                }
            }
        }
        for(int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
        
//利用JDK提供的工具也可以排序
int[] arr1 = new int[] {6,5,4,3,2}; Arrays.sort(arr1); for(int i = 0; i < arr1.length; i++) { System.out.println(arr1[i]); } } }

三、java數組