1.定義一個函數,函數功能是動態提取int[]中元素的最大值。java
2.定義一個函數,從數組中查詢指定的元素首次出現的位置。編程
3.定義函數,完成冒泡排序,大數下沉。數組
4.折半查找。安全
5.闡述ide
6.定義一個函數,實現矩陣的轉置.arr[i][j] == arr[j][i];//前提條件是正方的。函數
7.遍歷三維組數,橫向輸出三維數組的每個層。oop
8.定義一個類:Dog 有名稱 color age cry();大數據
9.闡述出來堆區,棧區,什麼時候出現溢出,如何解決。this
10.oopspa
---------------------------------------------------------------------
//1.定義一個函數,函數功能是動態提取int[]中元素的最大值。
class ArrayMaxDemo {
public static void main(String[] x) {
//System.out.println(Integer.MIN_VALUE); //最小整數值 Integer.MIN_VALUE;
//System.out.println(getMax(new int[0])); //arr.length == 0,打出數組不存在和 -1
int[] arr = new int[] { 2, 8, 1, 3, 7 };
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;最下面有了,不能寫在這裏,否則返回的就是第一個數值arr[0]的值了
}
//return temp;下面有了,不能寫在這裏,否則返回的就是第一個數值arr[0]的值了
}
return temp;//有返回值類型的,必需要有返回值
}
}
---------------------------------------------------------------------
//2.定義一個函數,從數組中查詢指定的元素首次出現的位置。
class SearchArray {
public static void main(String[] args) {
int[] arr = new int[] { 1, 2, 3, 5, 4 };
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;//成立的狀況,先初始化index值,
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;
//三種狀況貌似有了return,也用不着else if了
}
}
---------------------------------------------------------------------
//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;//a,b下標索引的最小和最大,m是若是找到對應數的索引值
int x = 5;
while (a <= b) {
m = (a + b) / 2;
if (arr[m] == x) {
System.out.println("Find it!,the index is " + m);//m的值是數組下標的索引值
break;
} else if (x < arr[m]) {////搜索範圍落在左邊
b = m - 1;
} else {//落在右邊
a = m + 1;
}
}
}
}
//用方法體現
//class HalfFind
//{
//public static void main(String[] x){
//System.out.println(find(new int[]{1,2,3,4,6,7,8},9));
//}
//
////大數據下沉冒泡
//public static int find(int[] arr,int x){
//int a = 0 ,b = arr.length - 1, m = 0 ;
//int mindex = 0 ;
//while(a <= b){
//mindex = ( a + b) / 2 ;
//m = arr[mindex] ;
//
//if(m == x){
//return mindex ;
//}
////搜索範圍落在左邊
//else if(m > x){
//b = mindex - 1 ;
//}
////落在右邊
//else{
//a = mindex + 1 ;
//}
//}
//return -1 ;
//
//}
//}
---------------------------------------------------------------------
//6.定義一個函數,實現矩陣的轉置.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);
}
// 轉置二位數組,其中1,7,13,19,25是不變的(arr[0][0],arr[1][1],arr[2][2],arr[3][3],arr[4][4]),轉置的話是轉一個三角形的範圍,不是正方形,否則前面先轉了,後面又變回去了
public static int[][] trans(int[][] arr) {
//轉置的話是轉一個三角形的範圍,不是正方形,否則前面先轉了,後面又變回去了,實際每行分別操做的是2-5,8-10,14-15,20------4行,最後一行不用操做
for (int i = 0; i < arr.length - 1; i++) {//外層,實際每行分別操做的是2-5,8-10,14-15,20------4行,arr.length - 1 最後一行(25那行)不用操做
for (int j = i ; j < arr[i].length; j++) {//好比1,不用操做,就直接j=i+1,操做2. 7不用操做,+1,就操做8
int temp = arr[i][j];//i和j相同轉換沒意義,上面就直接j=i+j,而不是int j = i,j < arr[i].length,不須要arr[i].length-1 ,這樣最後一行不會變換
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();
}
}
}
---------------------------------------------------------------------
//7.遍歷三維組數,橫向輸出三維數組的每個層。沒有5
/**
* 遍歷三維組數,橫向輸出三維數組的每個層
*/
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);
outThreeArray(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();
}
System.out.println("----------------------------------"+"\n"+"----------------------------------");
}
//正常輸入一個三維數組
public static void outThreeArray(int[][][] arr){
for(int i = 0;i < 3;i++){
for(int j = 0;j < 3;j++){
for(int k=0;k < 3;k++){
System.out.print(arr[i][j][k]+"\t");
}
System.out.println();
}
System.out.println("---------------------");
}
}
}
---------------------------------------------------------------------
闡述
1) 獲取數組的最大值,解決方法:遍歷整個數組,經過if條件判斷比較出最大值
2) 查詢數組中某個值,解決方法:遍歷整個數組,if條件判斷,查找到這個值時,跳出循環
3) 冒泡排序:將最小/最大的數,依次沉到數組的最後完成排序.
外層循環須要進行array.length-1次,內層循環須要比較array.length-i-1次.
4) 二分查找法:要求有序數組,經過比較中間值和查找值,肯定查找值所在位置(中間值的左或右),進行屢次循環查找找到值的真正所在.
5) 矩陣轉置問題:涉及矩陣初始化,賦值操做,轉置操做注意只需對左下正三角的值進行對位交換array[i][j]=array[j][i].
6) 三位數組:抽象爲魔方,分爲層,每層的每行,每層的每行的每列.經過循環控制,能夠橫向以及縱向打印每層.具體參見代碼.
7)面相對象:涉及面相對象類的定義,對象的生成,構造函數,修飾符,javabean技巧,對封裝的理解.
1) 獲取數組的最大值,解決方法:遍歷整個數組,經過if條件判斷比較出最大值
2) 查詢數組中某個值,解決方法:遍歷整個數組,if條件判斷,查找到這個值時,跳出循環
3) 冒泡排序:將最小/最大的數,依次沉到數組的最後完成排序.
外層循環須要進行array.length-1次,內層循環須要比較array.length-i-1次.
4) 二分查找法:要求有序數組,經過比較中間值和查找值,肯定查找值所在位置(中間值的左或右),進行屢次循環查找找到值的真正所在.
5) 矩陣轉置問題:涉及矩陣初始化,賦值操做,轉置操做注意只需對左下正三角的值進行對位交換array[i][j]=array[j][i].
6) 三位數組:抽象爲魔方,分爲層,每層的每行,每層的每行的每列.經過循環控制,能夠橫向以及縱向打印每層.具體參見代碼.
7)面相對象:涉及面相對象類的定義,對象的生成,構造函數,修飾符,javabean技巧,對封裝的理解.
---------------------------------------------------------------------
/*//8.定義一個類:Dog 有名稱 color age cry();*/
class DogDemo {
public static void main(String[] args) {
Dog job = new Dog("Jobboer", 7);
System.out.println("Name:" + job.getName() + " Age:" + job.getage());
job.setName("lala");
job.setAge(9);
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;
}
}
---------------------------------------------------------------------
9.闡述出來堆區,棧區,什麼時候出現溢出,如何解決。
堆區:保存對象以及成員變量棧區:保存方法以及局部變量
溢出條件:產生過多或者佔用內存很大的對象函數遞歸調用自身可能出現棧區溢出
如何解決:1.儘量不產生沒必要要的對象或成員變量1.遞歸操做要當心
2.設置JAVA虛擬機堆大小(java -Xms<size>) 2.採用非遞歸手段
---------------------------------------------------------------------
10.oop
面相對象:是相對面向過程而言的一種編程方式,將問題簡單化.
類:是對象的抽象.
對象:是類的具體實現.
實例:就是對象.
成員變量:對象的屬性變量.
成員函數:對象的方法.
public:用於修飾成員變量或者成員函數,表示公有,供其餘類調用.
private:用於修飾成員變量或者成員函數,表示私有,用於封裝,提升數據安全性,可經過set,get方法進行屬性的改變,獲取
構造函數:用於初始化對象.函數沒有返回值.
this:是對象的成員變量,指向當前的對象,用於類中方法引用當前對象.
static:靜態的,修飾成員變量,同類對象所共有,類也能夠引用靜態成員,靜態方法只能訪問靜態成員.