天天看點

黑馬程式員——java基礎——數組

------<a href="http://www.itheima.com" target="_blank" rel="external nofollow" target="blank">Java教育訓練、Android教育訓練、iOS教育訓練、.Net教育訓練</a>、期待與您交流! -------數組  ,二維數組,  對象數組

一:數組

    (1)數組:存儲同一種資料類型的多個元素的容器。

    (2)特點:每一個元素都有編号,從0開始,最大編号是長度-1。

             編号的專業叫法:索引

    (3)定義格式

        A:資料類型[] 數組名;  int [ ]  arr ;

        B:資料類型 數組名[];  int arr [ ] ;

        注意:推薦是用A方式,B方法就忘了吧。但是要能看懂

    (4)數組的初始化

        A:動态初始化

            隻給長度,系統給出預設值

            舉例:in t[ ] arr = new in t[3];

        B:靜态初始化

            給出值,系統決定長度

            舉例:int [ ] arr = new int [  ]{1,2,3};

            簡化版:int  [] arr = {1,2,3};

    (5)Java的記憶體配置設定

        A:棧 存儲局部變量

        B:堆 存儲所有new出來的

        C:方法區(面向對象部分詳細講解)

        D:本地方法區(系統相關)

        E:寄存器(CPU使用)

        注意:

            a:局部變量 在方法定義中或者方法聲明上定義的變量。

            b:棧記憶體和堆記憶體的差別

                棧:資料使用完畢,就消失。

                堆:每一個new出來的東西都有位址

                    每一個變量都有預設值

                        byte,short,int,long :0

                        float,double: 0.0

                        char :'\u0000'

                        boolean :false

                        引用類型: null

                    資料使用完畢後,在垃圾回收器空閑的時候回收。

    (6)數組的常見操作

        A:周遊

            方式1:

<span style="font-size:18px;"> public static void printArray(int[] arr) {
                    for(int x=0; x<arr.length; x++) {
                        System.out.println(arr[x]);
                    }
                }</span>
           

            方式2:

<span style="font-size:18px;"> public static void printArray(int[] arr) {
                    System.out.print("[");
                    for(int x=0; x<arr.length; x++) {
                        if(x == arr.length-1) {
                            System.out.println(arr[x]+"]");
                        }else {
                            System.out.println(arr[x]+", ");
                        }
                    }
                }</span>
           

        B:最值

            最大值:

<span style="font-size:18px;">  public static int getMax(int[] arr) {
                    int max = arr[0];
                    
                    for(int x=1; x<arr.length; x++) {
                        if(arr[x] > max) {
                            max = arr[x];
                        }
                    }
                    
                    return max;
                }</span>
           

            最小值:

<span style="font-size:18px;"> public static int getMin(int[] arr) {
                    int min = arr[0];
                    
                    for(int x=1; x<arr.length; x++) {
                        if(arr[x] < min) {
                            min = arr[x];
                        }
                    }
                    
                    return min;
                }</span>
           

        C:逆序

            方式1:

<span style="font-size:18px;"> public static void reverse(int[] arr) {
                    for(int x=0; x<arr.length/2; x++) {
                        int temp = arr[x];
                        arr[x] = arr[arr.length-1-x];
                        arr[arr.length-1-x] = temp;
                    }
                }</span>
           

            方式2:

<span style="font-size:18px;">public static void reverse(int[] arr) {
                    for(int start=0,end=arr.length-1; start<=end; start++,end--) {
                        int temp = arr[start];
                        arr[start] = arr[end];
                        arr[end] = temp;
                    }
                }</span>
           

        D:查表

<span style="font-size:18px;"> public static String getString(String[] strArray,int index) {
                    return strArray[index];
                }</span>
           

        E:基本查找

            方式1:

<span style="font-size:18px;">public static int getIndex(int[] arr,int value) {
                    for(int x=0; x<arr.length; x++) {
                        if(arr[x] == value) {
                            return x;
                        }
                    }
                    
                    return -1;
                }</span>
           

            方式2:

<span style="font-size:18px;">  public static int getIndex(int[] arr,int value) {
                    int index = -1;
                
                    for(int x=0; x<arr.length; x++) {
                        if(arr[x] == value) {
                            index = x;
                            break;
                        }
                    }
                    
                    return index;
                }</span>
           

二: 二維數組

    (1)元素是一維數組的數組。

    (2)格式:

        A:資料類型[ ][ ] 數組名 = new 資料類型[m][n];

        B:資料類型[ ][ ] 數組名 = new 資料類型[m][];

        C:資料類型[ ][ ] 數組名 = new 資料類型[ ][ ]{{...},{...},{...}};

        D:資料類型[ ][ ] 數組名 = {{...},{...},{...}};

    (3)案例:

        A:二維數組的周遊

<span style="font-size:18px;">class Array2Test {
	public static void main(String[] args) {
		//定義一個二維數組
		int[][] arr = {{1,2,3},{4,5,6},{7,8,9}};
		
		printArray2(arr);
	}
	
	
	public static void printArray2(int[][] arr) {
		for(int x=0; x<arr.length; x++) {
			for(int y=0; y<arr[x].length; y++) {
				System.out.print(arr[x][y]+" ");
			}
			System.out.println();
		}
	}
}</span>
           

        B:二維數組的求和

<span style="font-size:18px;">class Array2Test2 {
	public static void main(String[] args) {
		//二維數組
		int[][] arr = {{22,66,44},{77,33,88},{25,45,65},{11,66,99}};
		
		//定義一個求和變量sum,初始化值是0。
		int sum = 0;
		
		//通過周遊就可以得到每一個二維數組的元素。
		for(int x=0; x<arr.length; x++) {
			for(int y=0; y<arr[x].length; y++) {
				//把元素累加即可。
				sum += arr[x][y];
			}
		}
		
		//最後輸出sum,就是結果。
		System.out.println(sum);
	}
}</span>
           

        C:楊輝三角形

<span style="font-size:18px;">import java.util.Scanner;

class Array2Test3 {
	public static void main(String[] args) {
		//建立鍵盤錄入對象
		Scanner sc = new Scanner(System.in);
		
		//這個n的資料來自于鍵盤錄入。
		System.out.println("請輸入一個資料:");
		int n = sc.nextInt();
		
		//定義二維數組
		int[][] arr = new int[n][n];
		
		//給這個二維數組任何一行的第一列和最後一列指派為1
		for(int x=0; x<arr.length; x++) {
			arr[x][0] = 1; //任何一行第1列
			arr[x][x] = 1; //任何一行的最後1列
		}
		
		//按照規律給其他元素指派
		
		for(int x=2; x<arr.length; x++) {
			
			for(int y=1; y<=x-1; y++) {
				//每一個資料是它上一行的前一列和它上一行的本列之和。
				arr[x][y] = arr[x-1][y-1] + arr[x-1][y];
			}
		}
		
		//周遊這個二維數組
		for(int x=0; x<arr.length; x++) {
			for(int y=0; y<=x; y++) {
				System.out.print(arr[x][y]+"\t");
			}
			System.out.println();
		}
	}
}</span>
           

三: 對象數組

    (1)數組既可以存儲基本資料類型,也可以存儲引用類型。它存儲引用類型的時候的數組就叫對象數組。

    (2)案例:

        用數組存儲5個學生對象,并周遊數組

<span style="font-size:18px;">public class ObjectArrayDemo {
	public static void main(String[] args) {
		// 建立學生數組(對象數組)。
		Student[] students = new Student[5];

		// 建立5個學生對象,并指派。
		Student s1 = new Student("a", 22);
		Student s2 = new Student("b", 20);
		Student s3 = new Student("c", 25);
		Student s4 = new Student("d", 26);
		Student s5 = new Student("e", 30);

		// 把元素放到數組中。
		students[0] = s1;
		students[1] = s2;
		students[2] = s3;
		students[3] = s4;
		students[4] = s5;

		

		// 周遊
		for (int x = 0; x < students.length; x++) {
			//System.out.println(students[x]);
			
			Student s = students[x];
			System.out.println(s.getName()+"---"+s.getAge());
		}
	}
}
</span>