Java基礎第三天_數組

1.定義一個函數,函數功能是動態提取int[]中元素的最大值。

答:

class ArrayMaxDemo {

    public static void main(String[] x) {

        System.out.println(getMax(new int[0]));

        int[] arr = new int[] { 5, 7, 1, 3, 9 };

        System.out.println("Max is : " + getMax(arr));

 

    }

 

    public static int getMax(int[] arr) {

        if (arr == null || arr.length == 0) {

            System.out.println("數組不存在!");

            return -1;

        }

        int temp = Integer.MIN_VALUE;

        for (int i = 0; i < arr.length; i++) {

            if (temp < arr[i]) {

                temp = arr[i];

            }

        }

        return temp;

    }

}

 

2.定義一個函數,從數組中查詢指定的元素首次出現的位置。

答:

class SearchArray {

    public static void main(String[] args) {

    int[] arr = new int[] { 1, 2, 3, 4, 5 };

    int index = searchArray(3, arr);

    }

    public static int searchArray(int number, int[] array) {

        if (array == null || array.length == 0) {

            System.out.println("array is null or array lenth is 0!");

            return -1;

        }

        int index = -1;

        for (int i = 0; i < array.length; i++) {

            if (array[i] == number) {

            index = i;

            System.out.println("Search " + number + " succeed! the index is " + index);

            return index;

            }

        }

    System.out.println("can not searched" + number);

    return -1;

    }

}

 

3.定義函數,完成冒泡排序,大數下沉。

答:

//數組升序冒泡排列

class BubbleSort {

    public static void main(String[] xargs) {

        int[] arr = sort(new int[] { 3, 1, 5, 9, 7 });

        out(arr);

    }

    public static int[] sort(int[] array) {

        for (int i = 0; i < array.length - 1; i++)

            for (int j = 0; j < array.length - 1 - i; j++) {

                int temp = 0;

                if (array[j] > array[j + 1]) {

                    temp = array[j];

                    array[j] = array[j + 1];

                    array[j + 1] = temp;

                }

            }

        return array;

        }

    public static void out(int[] array) {

        for (int i = 0; i < array.length; i++) {

            System.out.print(array[i] + "\t");

        }

        System.out.println();

    }

}

 

4.折半查找。

答:

/**

 * 折半查找/二分查找

 */

class HalfFind {

    public static void main(String[] args) {

        int[] arr = new int[] { 1, 2, 3, 4, 5, 6, 7, 8 };

        int a = 0, b = arr.length - 1, m = 0;

        int x = 5;

        while (a <= b) {

            m = (a + b) / 2;

            if (arr[m] == x) {

                System.out.println("Find it!,the index is " + m);

                break;

 

            } else if (x < arr[m]) {

                b = m - 1;

            } else {

                a = m + 1;

                }

        }

    }

}

 

5.定義一個函數,實現矩陣的轉置.arr[i][j] == arr[j][i];//前提條件是正方的。

答:

/**

 * 計算數組的轉置

 */

class ArrayTransDemo {

    public static void main(String[] args) {

        int[][] arr = { { 1, 2, 3, 4, 5 }, { 6, 7, 8, 9, 10 },{ 11, 12, 13, 14, 15 }, { 16, 17, 18, 19, 20 },{ 21, 22, 23, 24, 25 } };

        out(arr);

        System.out.println("---------------");

        arr = trans(arr);

        out(arr);

    }

 

// 轉置

    public static int[][] trans(int[][] arr) {

        for (int i = 0; i < arr.length - 1; i++) {

            for (int j = i + 1; j < arr[i].length; j++) {

                int temp = arr[i][j];

                arr[i][j] = arr[j][i];

                arr[j][i] = temp;

            }

        }

    return arr;

    }

 

    public static void out(int[][] arr) {

        for (int i = 0; i < arr.length; i++) {

            for (int j = 0; j < arr[i].length; j++) {

                System.out.print(arr[i][j] + "\t");

            }

        System.out.println();

        }

    }

}

 

6.遍歷三維組數,橫向輸出三維數組的每一個層。

答:

/**

 * 遍歷三維組數,橫向輸出三維數組的每一個層

 */

class OutHorizontalDemo {

    public static void main(String[] args) {

        int[][][] arrr = { { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } },{ { 10, 11, 12 }, { 13, 14, 15 }, { 16, 17, 18 } },{ { 19, 20, 21 }, { 22, 23, 24 }, { 25, 26, 27 } } };

        outHor(arrr);

    }

 

// 橫向輸出三維數組

    public static void outHor(int[][][] arr) {

// 循環行數,每個二維數組的長度

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

// 輸出所有列

            for (int j = 0; j < arr.length; j++) {

// 輸出每個層上的第i行的元素.

                for (int k = 0; k < arr[j][i].length; k++) {

                    System.out.print(arr[j][i][k] + "\t");

                }

            System.out.print(" | ");

            }

        System.out.println();

        }

    }

}

 

7.定義一個類:Dog 有名稱 color age cry();

答:

class DogDemo {

    public static void main(String[] args) {

        Dog job = new Dog("Job", 2);

        System.out.println("Name:" + job.getName() + " Age:" + job.getage());

        job.setName("lala");

        job.setAge(3);

        System.out.println("Name:" + job.getName() + " Age:" + job.getage());

    }

}

 

class Dog {

    private String name;

    private int age;

    public void cry() {

        System.out.println("wangwang~!");

    } 

    public Dog(String name, int age) {

        this.name = name;

        this.age = age;

    } 

    public void setName(String name) {

        this.name = name;

    }

    public void setAge(int age) {

        this.age = age;

    }

    public String getName() {

        return this.name;

    } 

    public int getage() {

        return this.age;

    }

}

 

8.闡述

答:

1) 獲取數組的最大值,解決方法:遍歷整個數組,通過if條件判斷比較出最大值

2) 查詢數組中某個值,解決方法:遍歷整個數組,if條件判斷,查找到這個值時,跳出循環

3) 冒泡排序:將最小/最大的數,依次沉到數組的最後完成排序.

外層循環需要進行array.length-1次,內層循環需要比較array.length-i-1次.

4) 二分查找法:要求有序數組,通過比較中間值和查找值,確定查找值所在位置(中間值的左或右),進行多次循環查找找到值的真正所在.

5) 矩陣轉置問題:涉及矩陣初始化,賦值操作,轉置操作注意只需對左下正三角的值進行對位交換array[i][j]=array[j][i].

6) 三位數組:抽象爲魔方,分爲層,每層的每行,每層的每行的每列.通過循環控制,可以橫向以及縱向打印每層.具體參見代碼.

7)面相對象:涉及面相對象類的定義,對象的生成,構造函數,修飾符,javabean技巧,對封裝的理解.

 

9.闡述出來堆區,棧區,何時出現溢出,如何解決。

答:

堆區:保存對象以及成員變量  

棧區:保存方法以及局部變量

溢出條件:產生過多或者佔用內存很大的對象函數遞歸調用自身可能出現棧區溢出

如何解決:1.儘可能不產生不必要的對象或成員變量。遞歸操作要小心

         2.設置JAVA虛擬機堆大小(java -Xms<size>)。採用非遞歸手段

 

10.oop

答:

面相對象:是相對面向過程而言的一種編程方式,將問題簡單化.

類:是對象的抽象.

對象:是類的具體實現.

實例:就是對象.

成員變量:對象的屬性變量.

成員函數:對象的方法.

public:用於修飾成員變量或者成員函數,表示公有,供其他類調用.

private:用於修飾成員變量或者成員函數,表示私有,用於封裝,提高數據安全性,可通過set,get方法進行屬性的改變,獲取

構造函數:用於初始化對象.函數沒有返回值.

this:是對象的成員變量,指向當前的對象,用於類中方法引用當前對象.

static:靜態的,修飾成員變量,同類對象所共有,類也可以引用靜態成員,靜態方法只能訪問靜態成員.


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