數(shù)組操作
1.數(shù)組地址轉(zhuǎn)移問題【難點(diǎn)】

public static void main(String [] args) {
int[] arr1 = new int[10];
int[] arr2 = new int[10];
arr1[0] = 10;
arr2[0] = 20;
System.out.println("arr1[0]:" + arr1[0]);
System.out.println("arr2[0]:" + arr2[0]);
arr1[5] = 50;
arr2 = arr1;
arr1[0] = 30;
arr2[0] = 100;
System.out.println("arr1[0]:" + arr1[0]);
System.out.println("arr2[0]:" + arr2[0]);
System.out.println("arr2[5]:" + arr2[5]);
問題總結(jié):
1.
? ? int[] arr1 = new int[20];
? ? int[] arr2 = new int[10];
? ? arr1 = arr2;
? ? arr1[15] ???;錯誤的
? ? arr1和arr2執(zhí)行的空間為同一個數(shù)組空間
2.
????int[] arr1 = new int[10];
????int[] arr2 = new int[10];
????arr1[0] = 10;
????arr2[0] = 20;
????arr2[5] = 100;
????arr1[0] = arr2[0];
賦值操作完成的是對于元素直接內(nèi)容的轉(zhuǎn)換,不涉及數(shù)組地址轉(zhuǎn)移
2.數(shù)組作為方法的參數(shù)
public static void main(String[] args) {
}
/*
(String[] args)
????????String[] args就是給數(shù)組作為方法參數(shù)格式,這里main方法需要的參數(shù)是一個String類型的數(shù)組
格式:
????????public static返回值類型方法名(數(shù)據(jù)類型[] 數(shù)組參數(shù)名)
數(shù)組是可以作為方法的返回值
數(shù)組作為參數(shù)分析過程
a.在一個int類型數(shù)組中,存儲的內(nèi)容是1 ~ 10
????????int[] arr = {10, 8, 35, 67, 31, 25, 11, 30,28, 99};
????????使用代碼找出,元素== 30所在下標(biāo)位置
方法聲明:
????????public static int indexOf(int[] arr, int find)
方法實(shí)現(xiàn)和文檔注釋
/**
*在指定數(shù)組arr中,查詢指定元素find所在的下標(biāo)位置,找到返回值大于等于0,沒有找到
*返回-1
*
* @param arr 查詢數(shù)據(jù)的源數(shù)據(jù)數(shù)組,int類型數(shù)組
* @param find指定在數(shù)組中查詢的數(shù)據(jù),也是對應(yīng)int類型
* @return找到指定元素,返回值大于等于0,否則返回-1
*/
? ? ? ?public static int indexOf(int[] arr,intfind) {
????????//
????????假設(shè)查詢的數(shù)據(jù)不存在
????????int index = -1;
????????//遍歷整個數(shù)組
????????for (int i = 0; i < arr.length; i++) {
????????//發(fā)現(xiàn)數(shù)組中下標(biāo)為i的元素和find一致,找到內(nèi)容,保存下標(biāo)
????????????????if (find == arr[i]) {
????????????????index = i;
????????????????//終止循環(huán)
????????????????break;
? ? ? ? ? ? ? ?}
? ????????? }
????????????//返回index中保存的數(shù)據(jù)
????????????returnindex;
????????}
方法驗(yàn)證和傳參方式
class Demo {
????public static void main(String[] args) {
????????int[] array = {10,8,35,67,31,25,11,30,28,99};
????????int find = 13;
????????//調(diào)用的方法需要數(shù)組作為方法的參數(shù),
????????//這里傳入【數(shù)組名】
????????int index = indexOf (array,find);
????????if (index >= 0) {
????????????System.out.println("index : " + index);
????????} else {
????????????????System.out.println("Not Found!");
? ? ? ? }
}
/**
*在指定數(shù)組arr中,查詢指定元素find所在的下標(biāo)位置,找到返回值大于等于0,沒有找到
*返回-1
*
* @param arr 查詢數(shù)據(jù)的源數(shù)據(jù)數(shù)組,int類型數(shù)組
* @param find指定在數(shù)組中查詢的數(shù)據(jù),也是對應(yīng)int類型
* @return找到指定元素,返回值大于等于0,否則返回-1
*/
????public static int indexOf(int[] arr, int find){
????????//假設(shè)查詢的數(shù)據(jù)不存在
????????int index = -1;
????????//遍歷整個數(shù)組
????????for (int i = 0; i < arr.length; i++) {
????????????//發(fā)現(xiàn)數(shù)組中下標(biāo)為i的元素和find一致,找到內(nèi)容,保存下標(biāo)
????????????if (find ==arr[i]) {
????????????????index = i;
????????????????//終止循環(huán)
????????????????break;
????????????}
????????}
????????//返回index中保存的數(shù)據(jù)
????????return index;
????}
}
方法運(yùn)行內(nèi)存圖

總結(jié)
1. 數(shù)組作為方法參數(shù)的固定格式
????????(數(shù)據(jù)類型[] 數(shù)組參數(shù)名)
2.數(shù)組作為方法的實(shí)際參數(shù)的固定格式
????????(數(shù)組名)
3.數(shù)組名作為方法的參數(shù),實(shí)際傳遞的是數(shù)組空間首地址,就是和數(shù)組地址轉(zhuǎn)移問題是一致的
4.方法執(zhí)行需要參數(shù),如果沒有給予對應(yīng)格式的實(shí)際參數(shù),直接報錯