java代碼庫02——數組
拒絕窘境,當你不停滯不前的時候,就已經在退步了。
Akino·杜菲 版本聲明:原創內容,請勿轉載,否則將追究法律責任!
1.基本數據類型的數組變量,直接打印的結果:
打印的內容是[I@7852e922這種地址值,註意唯一不同的是,println這個方法對字符型的數組有特殊的處理方式:重載這個方法的結果是打印數組裏的所有字符!
package com.test; public class Practise2 { public static void main(String[] args) { byte[] a1 = new byte[2];View Codeshort[] a2 = new short[2]; char[] a3 = {‘a‘,‘b‘,‘6‘}; int[] a4 = new int[5]; float[] a5 = new float[4]; long[] a6 = new long[5]; boolean[] a7 = new boolean[3]; double[] a8 = new double[3]; System.out.println(a1); System.out.println(a2); System.out.println(a3); System.out.println(a4); System.out.println(a5); System.out.println(a6); System.out.println(a7); System.out.println(a8); } }
2.數組的默認值
註意:數組的默認值是0,註意如何給數組賦值
/* 驗證: 1.數組的默認值: 2.數組元素的訪問方式: 數組名[索引值] 練習:定義基本數據類型的數組,有2個元素.考察他們的默認值: */ public class ArrayDemo3{ public static void main(String[] args){ //定義一個數組 int[] arr = new int[3]; //在堆空間中開辟3個空間,每個空間可以存放一個int型元素.初始值為0.View Code//然後把首元素的地址(也就是整個數組的地址)賦值給棧空間中的變量arr //驗證默認值: System.out.println(arr[0]); System.out.println(arr[1]); System.out.println(arr[2]); //如何對數組元素賦值: arr[2] = 10; System.out.println(arr[2]); arr[2] = 20; System.out.println(arr[2]); } }
3.數組的默認值
註意:引用數組默認值為null,就是String[ ]的默認值是null,剩下是0.0,0,false之類
/* 考察基本數據類型和引用數據類型的數組的默認值! 整數:0 浮點數:0.0 布爾型:false 引用數據類型:null */ public class ArrayDemo4{ public static void main(String[] args){ byte[] arr1 = new byte[2]; System.out.println(arr1[0]); System.out.println(arr1[1]); System.out.println("------------------"); char[] arr2 = new char[2]; System.out.println(arr2[0]); System.out.println(arr2[1]); System.out.println("------------------"); short[] arr3 = new short[2]; System.out.println(arr3[0]); System.out.println(arr3[1]); System.out.println("------------------"); int[] arr4 = new int[2]; System.out.println(arr4[0]); System.out.println(arr4[1]); System.out.println("------------------"); long[] arr5 = new long[2]; System.out.println(arr5[0]); System.out.println(arr5[1]); System.out.println("------------------"); boolean[] arr6 = new boolean[2]; System.out.println(arr6[0]); System.out.println(arr6[1]); System.out.println("------------------"); float[] arr7 = new float[2]; System.out.println(arr7[0]); System.out.println(arr7[1]); System.out.println("------------------"); double[] arr8 = new double[2]; System.out.println(arr8[0]); System.out.println(arr8[1]); System.out.println("------------------"); //定義字符串數組 String[] arr9 = new String[2]; System.out.println(arr9[0]); System.out.println(arr9[1]); } }View Code
4.數組的遍歷(1)
註意:也可以使用增強for更容易
package com.test; public class Practise4 { public static void main(String[] args) { int[] arr = { 1, 2, 3, 4, 56, 6, 5, 4, 2, 1, 1 }; for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]); } } }View Code
5.數組的遍歷(2)
註意:數組遍歷每個數字之間打個逗號,最後一個數字不需要有逗號,所以需要加入一個判斷,就是數組長度 i !=arr.length的時候打逗號
package com.test; public class Practise5 { public static void main(String[] args) { int[] arr = {1,2,3,5,6}; printArray(arr); } public static void printArray(int[] arr) { for (int i = 0; i < arr.length; i++) { System.out.print(arr[i]); if(i!=arr.length-1){ System.out.print(","); } } } }View Code
6.自定義數組的長度,輸入最大值和最小值,並自定義取的長度
方式:定義方法打印、最大值、最小值,然後錄入兩個數據,求兩個數據之間的隨機個數的數值,利用了math.random()*(max-min+1)+min
/* 數組長度和元素都是隨機產生.求最值: 元素的範圍是鍵盤錄入的. */ import java.util.Scanner; public class ArrayDemo10{ public static void main(String[] args){ Scanner s = new Scanner(System.in); System.out.print("輸入元素的最小值: "); int min = s.nextInt(); System.out.print("輸入元素的最大值: "); int max = s.nextInt(); //隨機產生一個數,當成數組的長度 int len = (int)(Math.random() * 50 + 1); //創建數組 int[] arr = new int[len]; //遍歷數組,用隨機數給元素賦值 for(int i = 0;i<arr.length;i++){ arr[i] = (int)(Math.random() * (max - min + 1) + min); } //打印隨機數組 System.out.println("產生的隨機數組是: "); printArray(arr); //求最值: System.out.println("最大值是: "+ getMax(arr)); System.out.println("最小值是: "+ getMin(arr)); } //自定義方法,打印數組的元素 public static void printArray(int[] arr){ for(int i = 0;i<arr.length;i++){ System.out.print(arr[i]); if(i != arr.length - 1){ System.out.print(","); } } //最好在最後加一個換行, System.out.println(); } //getMax public static int getMax(int[] arr){ int max = arr[0]; for(int i = 1;i<arr.length;i++){ if(arr[i] > max){ max = arr[i]; } } return max; } //getMin public static int getMin(int[] arr){ int min = arr[0]; for(int i = 1;i<arr.length;i++){ if(arr[i] < min){ min = arr[i]; } } return min; } }View Code
7.獲取指定元素的第一次出現的索引值
思路:定義一個方法判斷是否相等返回i還是-1,main函數中加個條件判斷,剛才返回的值要是i就有,打印一下,要是-1就每一,索引不會有負數的。
/* *普通查找:獲取指定元素第一次出現的索引 * 普通查找:就是從前往後遍歷查找 * 二分查找:要求數組必須是已經排好序的 * */ package com.test; public class Practise8 { public static void main(String[] args) { int[] arr = {1,2,3,4,5,30,6}; int value = 30; int res = getIndex(arr, value); if(res==-1){ System.out.println("沒有找到"); }else{ System.out.println(value+"第一次出現的索引值是:"+res); } } public static int getIndex(int[] arr,int value){ for(int i = 0 ;i<arr.length;i++){ if(arr[i] == value){ return i; } } return -1; } }View Code
8.數組的復制
思路:遍歷原數組,然後讓新數組的每個元素和原數組相等,然後打印一下,當然java.lang包下也已經為我們寫好了一個數組復制,arr.clone();
/* 定義方法實現:數組的復制:重點考察是否需要返回值 既然是復制,說明產生了一個新的數組.這個新的數組和源數組的長度一致,各個元素都相同. 返回值:就是產生的新的數組!! 類型是:int[] */ public class ArrayDemo12{ public static void main(String[] args){ int[] src = {9,5,2,7}; //調用方法獲取一個src的副本 int[] copy = copyArray(src); // printArray(src); printArray(copy); // System.out.println(src == copy);//false } // public static void printArray(int[] arr){ for(int i = 0;i<arr.length;i++){ System.out.print(arr[i]); if(i != arr.length -1){ System.out.print(","); } } System.out.println(); } //用於實現:數組的復制 //返回值類型:int[] //形參列表:int[] src public static int[] copyArray(int[] src){ //創建一個和src一樣大的數組,遍歷源數組,用源數組的元素給新數組元素賦值. int[] copy = new int[src.length]; //遍歷 for(int i = 0;i<src.length;i++){ copy[i] = src[i]; } // return copy; } }View Code
8.定義方法求階乘
思路:for循環,階乘思想,就是階乘的值在遍歷過程中 i 要從1開始取。
/* 1.定義方法,用於計算一個int數值的階乘 //返回值類型:int //形參列表:int value */ public class HomeworkDemo1{ public static void main(String[] abc){ // int res = getNum(5); System.out.println(res); // System.out.println(getNum(5)); } //返回值類型:int //形參列表:int value public static int getNum(int value){ //求階乘思想: int res = 1; for(int i = 1;i<=value;i++){ res *= i; } return res; } }View Code
9.指定起始值和最終值,隨機取一個數
思路:利用math.random(),
/* * 練習:從鍵盤錄入起始值和結束值,獲取指定範圍的隨機值 * 定義方法,用於獲取指定範圍內的一個隨機值(int) * */ package com.test; import java.util.Scanner; public class Practise11 { public static void main(String[] args) { Scanner scanner= new Scanner(System.in); System.out.println("請輸入起始值: "); int a = scanner.nextInt(); System.out.println("請輸入最終值: "); int b = scanner.nextInt(); System.out.println("隨機值是 "+getRandom(a, b)); } public static int getRandom(int a,int b){ return (int)(Math.random()*(a-b+1)+b); } }View Code
你不願意改變的時候,往往就是你最需要改變的時候。讓你變得更加專業,成為一個it精英。只有經歷才能讓我們更加認識自己,不經歷歷練就無法看見陽光。 歡迎加入BigBang高級大數據之路,qq群號:945694891。
java代碼庫02——數組