【0 基礎學 Java】(十)數組習題

碼字不易,對你有幫助 點贊/轉發/關注 支持一下作者

微信搜公衆號:不會編程的程序圓

看更多幹貨,獲取第一時間更新

看更多示例和代碼: https://github.com/hairrrrr/EasyJava,歡迎 star

1.數組轉字符串
class Test1{
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4};
        String newArr = Arrays.toString(arr);
        System.out.println("newArr = " + newArr);
    }

}
Array.toString 實現
class Test1{
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4};
        System.out.println(toSting(arr));
    }
    public static String toSting(int[] arr){
        String newArr = "[";
        for(int i = 0; i <= arr.length - 1; i++){
            //字符串拼接操作
            newArr += arr[i];
            //除了數組最後一個元素,每個元素後面都有 ", "
            if(i != arr.length - 1){
                newArr += ", ";
            }
        }
        newArr += "]";
        return newArr;
    }
}

2. 數組拷貝
class Test1{
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4};
        int[] newArr1 = arr;
        int[] newArr2 = Arrays.copyOf(arr, arr.length);
        System.out.println(Arrays.toString(newArr1));
        System.out.println(Arrays.toString(newArr2));

        arr[0] = 10;
        System.out.println("arr = " + Arrays.toString(arr));
        System.out.println("newArr1 = " + Arrays.toString(newArr1));// 直接賦值操作新建的數組的元素也被改變
        System.out.println("newArr2 = " + Arrays.toString(newArr2));// 通過copyOf 新建的數組的元素沒有改變

        int[] newArr3 = Arrays.copyOfRange(arr,0,3);//左閉右開區間 【0,3)
        System.out.println("newArr3 = " + Arrays.toString(newArr3));
    }

}
//輸出:
[1, 2, 3, 4]
[1, 2, 3, 4]
arr = [10, 2, 3, 4]
newArr1 = [10, 2, 3, 4]
newArr2 = [1, 2, 3, 4]
newArr3 = [10, 2, 3]

注意: 相比於 newArr = arr 這樣的賦值, copyOf 是將數組進行了 深拷貝, 即又創建了一個數組對象, 拷貝原有數組中的所有元素到新數組中. 因此, 修改原數組, 不會影響到新數組

copyOf 實現
   public static int[] copyOf(int[] arr) {
        int[] ret = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            ret[i] = arr[i];
        }
        return ret;
    }
3. 找數組中的最大元素
class Test1{
    public static void main(String[] args) {
        int[] arr = {3, 4, 2, 1};
        System.out.println(findMax(arr));
    }
    public static int findMax (int[] arr) {
        int max = arr[0];
        for(int i = 1; i < arr.length; i++){
            if(max < arr[i]){
                max = arr[i];
            }
        }
        return max;
    }

}
4. 求數組元素的平均值
class Test1{
    public static void main(String[] args) {
        int[] arr = {3, 4, 2, 1};
        System.out.println(average(arr));
    }
    public static double average (int[] arr) {
        int sum = 0;
        for(int i = 0; i < arr.length; i++){
            sum += arr[i];
        }
        
        return (double)sum / arr.length;
    }

}
5. 順序查找
class Test1{
    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        int[] arr = {3, 4, 2, 1};
        int isExist = scan.nextInt();
        System.out.println(find(arr, isExist));
    }
    public static int find(int[] arr, int num) {

        for(int i = 0; i < arr.length; i++){
            if(num == arr[i]){
                return i;
            }
        }

        return -1;//表示沒找到
    }

}
6. 查找數組中的指定元素(二分查找)
public class Test1 {

    public static void main(String[] args) {
        int[] arr = new int[]{1, 2, 3, 4, 5, 6};
        int indexOfArr = binarySearch(arr, 3);
        if(indexOfArr != -1) {
            System.out.println(indexOfArr);
        }else{
            System.out.println("沒找到!");
        }

    }
    public static int binarySearch(int[] arr, int target){
        int left = 0;
        int right = arr.length - 1;

        while(left <= right) {
            int mid = (left + right) / 2;
            if (target > arr[mid]) {
                left = mid + 1;
            }
            if (target < arr[mid]) {
                right = mid - 1;
            }
            if(target == arr[mid]){
                return mid;
            }
        }
        return -1;

    }
}
import java.util.Arrays;

class Test1{
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4};
        System.out.println(Arrays.binarySearch(arr, 3);
    }

}
//需要注意的是,如果沒有找到目標數,Array.binarySearch 會返回 -(low + 1)
//這時,low 的值只可能是 -1 或 arr.length + 1                           
7. 檢查數組的有序性

給定一個整型數組,判斷該數組是否是有序的。

class Test1{
    public static void main(String[] args) {
        int[] arr = {1, 2, 5, 4};
        System.out.println(isSorted(arr));
    }
    public static boolean isSorted(int[] arr){
        for(int i = 0; i < arr.length - 1; i++){
            if(arr[i] > arr[i + 1]){
                return false;
            }
        }
        return true;
    }
}
8. 數組排序(冒泡排序)
import java.util.Arrays;

class Test1{
    public static void main(String[] args) {
        int[] arr = {9, 5, 2 ,7};
        bubbleSort(arr);
        System.out.println(Arrays.toString(arr));
    }
    public static void bubbleSort(int[] arr){
        for(int i = 0; i < arr.length - 1; i++){
            boolean flg = true;
            for(int j = 0; j < arr.length - 1 - i; j++){
                if(arr[j] > arr[j + 1]) {
                    int tmp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = tmp;
                    flg = false;
                }
            }
            //數組已經達到有序
            if(flg){
                break;
            }
        }
    }
}

我們也可以用 Java 已經寫好的方法:

import java.util.Arrays;

class Test1{
    public static void main(String[] args) {
        int[] arr = {9, 5, 2, 7};
        Arrays.sort(arr);
        System.out.println(Arrays.toString(arr));
    }
}
9. 數組逆序
import java.util.Arrays;

class Test1{
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4};
        reverse(arr);
        System.out.println(Arrays.toString(arr));
    }
    public static void reverse(int[] arr){
        int left = 0;
        int right = arr.length - 1;
        while(left < right){
            int tmp = arr[left];
            arr[left] = arr[right];
            arr[right] = tmp;
            left++;
            right--;
        }
    }
}
10. 偶數放在數組的前半部分
import java.util.Arrays;

//將偶數置於數組前端
class Test1{
    public static void main(String[] args) {

        int[] arr = {9, 5, 2, 7};
        int left = 0;
        int right = arr.length - 1;
        while(left < right){
            //從左端尋找奇數
            while(left < right && arr[left] % 2 == 0){
                left++;
            }
            //從右端開始找偶數
            while(left < right && arr[right] % 2 != 0){
                right--;
            }
            int tmp = arr[left];
            arr[left] = arr[right];
            arr[right] = tmp;
        }
        System.out.println(Arrays.toString(arr));
    }

}

以上就是本次的內容。

如果文章有錯誤歡迎指正和補充,感謝!

關注我,看更多幹貨!

我是程序圓,我們下次再見。

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