20200226java學習之路之數組

一: 數組

  1. 數組就是容器,同一種類型數據的集合
  2. 數組的格式1:數組類型[] 數組名 = new 數組類型[元素個數或數組長度]
  3. 格式1:int [ ] arr =new int [5];
public class TestArry{
	public static void main(String[] args){
		int [] arr=new int [3];
		arr[0]=89;
		System.out.println(arr[0]);
	}
}

運行結果:

4.
4.數組的格式2:int 【】arr=new int [ ]{1,3,5,7}

public class TestArry{
	public static void main(String[] args){
		int [] arr=new int [3];
		arr[0]=89;
		System.out.println(arr[0]);
		System.out.println("-------------");
		testArry1();
	}


	public static void testArry1(){
		int [] arr={89,34,270,17};
		System.out.println(arr[2]);
		return ;
	}
}

結果:
在這裏插入圖片描述
5. 數組的好處:方便操作元素,可給數組元素從0開始編號

二:數組內存的劃分

1.寄存器
2.本地方法區
3.方法區
4.棧內存
1.存儲的局部變量
2.而且變量所屬作用域一旦結束,該變量則自動消失
5.堆內存

  1. 存儲是數組和對象(數組就是對象)----凡是new建立在堆中
  2. 特點:每一個實體都有首地址值
  3. 堆內存中每一個變量都有默認初始化值,根據類型不同而不同。(整數0,小數0.0或0.0f,boolean false,char ‘\u0000’------u代表UNICORN代碼–0000代表兩個字節)

數組的例題

1.打印數組中數據

public class TestArry{
	public static void main(String[] args){
		int [] arr=new int [3];
		arr[0]=89;
		System.out.println(arr[0]);
		System.out.println("-------------");
		testArry1();
	}


	public static void testArry1(){
		int [] arr={89,34,270,17};//存儲已知具體數值
		System.out.println(arr[2]);
		System.out.println("-------------");
		for(int x=0;x<4;x++){  //x<arr.length若數據多時使用
			System.out.println(arr[x]);//獲取所有的元素
		}
	}
}

運行結果:
在這裏插入圖片描述

public class TestArry2{
	public static void main(String[] args){
	int age=3;
		System.out.println(age);
		System.out.println("-------------");

	
	int [] arr=new int [3];
		arr[0]=89;
		System.out.println(arr[ 0]);
		System.out.println("-------------");
		for(int x=0;x<3;x++){
			System.out.println(arr[x]);
			
		}
		System.out.println("-------------");
		testArry3();
	}	
		

		public static void testArry3(){
			int [] arr={89,34,99,66};
			System.out.println("length:"+arr.length);
			for(int x=0;x<arr.length;x++){//x<arr.length若數據多時使用
				System.out.println("arr["+x+"]="+arr[x]+";");
				/*for(int x=arr.length-1;x>=0;x--){
					System.out.println("arr["+x+"]="+arr[x]+";");
				}
				可以用來反向輸出最後的值				*/
			}
		}	
}

運行結果:
在這裏插入圖片描述

數組之最值的實例

/*
需求一:獲取數組的最大值(  最值  )
思路: 1,需要進行比較,並定義變量記錄住每次比較後較大的值
      2,對數組進行遍歷,取出並和變量的元素進行比較,若遍歷後的數據大於變量記錄的元素,就用變量記錄此最大值
      3,遍歷後的結果該變量記錄就是最大值
定義一個函數功能,獲取數組最大的數
明確是否有結果? 有,數組中的元素,int 類型
明確二,是否有未知內容?  有, 數組中最大值數據
*/

public class TestArrayGetMax{
	public static void main(String[] args){
		int[] arr= {23,45,67,98,187};
		//int[] tempArr={};
		//int tempMax=getMax(tempArr);不能是空數組,否則系統報錯,超出邊界
		int max=getMax(arr)	;	
		System.out.println("max="+max);
	    int maxIndexOfValue=getMax2(arr);
	}


	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];
				//System.out.println("max="+max);
		}
		return max;
	}


	public static int getMax2(int[] arr){
		int maxIndex=0;//角標
		for(int x=0;x<arr.length;x++){
			if (arr[x]>maxIndex)
				maxIndex=x;
			System.out.println("maxIndex="+maxIndex);
		}
		return arr[maxIndex];
	}


	public static void testFunc(int first){
		System.out.println("first="+first);
		}
	}


    
	public static void  print(int a,int count){
		for (int i=0;i<count;i++) {
			System.out.println(a);
		}
		
	}
}

運行結果:
在這裏插入圖片描述

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