java数组-如何在一堆数据中使用数组!
數組
1.類型一致的一組數據,其實相當于集合概念。
?數組描述的是相同類型的若干個數據,按照一定的先后次序排列組合而成。其中,每一個數據稱作一個數組元素(item),每個數組元素可以通過一個下標/索引來(index)訪問它們.
?
1)數組變量:是引用類型變量(不是基本變量)引用變量通過數組的內存地址位置引用了一個數組(數組對象)。
2) ①數據類型 變量[]?= new int[]{值1,值2,…}
②數據類型[] ary = new int[]{值1,值2,…}? --> 推薦這種寫法
int[] ary1 = new int[]{2,3,4}; //創建數組時候直接初始化元素
int[] ary2 = {2,3,4}; //數組靜態初始化,只能在聲明變量的同時直接賦值
//ary2 = {4,5,6}; //編譯錯誤,不能用于賦值等情況
ary2 = new int[]{4,5,6};
?
3)數組元素的訪問:①數組長度:長度使用屬性訪問,ary.length 獲取數組下標。②數組下標:范圍:0 ~ length-1就是[0,length),超范圍訪問會出現下標越界異常。③使用[index] 訪問數組元素:ary[2]。④迭代:就是將數組元素逐一處理一遍的方法。
?
案例:聲明一個數組申請空間并賦值
1 public class Test01{ 2 public static void main(String[] args){ 3 // 聲明一個數組 4 // int ary[]; 5 6 int a; 7 8 // 【1】聲明數組變量 9 int[] ary; 10 11 // 【2】給數組變量分配空間 12 // 給arr申請了5個連續的整形的int空間。 13 ary = new int[5]; 14 15 // 【3】給每個空間賦值 16 ary[0] = 10; 17 ary[1] = 20; 18 ary[2] = 30; 19 ary[3] = 40; 20 ary[4] = 50; 21 22 // 【4】訪問元素 23 System.out.println(ary[0]); 24 System.out.println(ary[1]); 25 System.out.println(ary[2]); 26 27 // System.out.println(arr[5]); 28 } 29 }?
2.數組的內存空間
?
① jvm根據后面值的個數申請空間并把值賦值到對于空間
1 public class Test02{ 2 public static void main(String[] args){ 3 4 // 【1】數組的聲明方式 5 6 int[] ary = new int[5]; 7 ary[0] = 10; 8 ary[1] = 20; 9 System.out.println(ary); 10 11 // 【2】值聲明 12 int[] ary2; 13 ary2 = new int[]{10,20,30,40,50}; 14 15 // int[] ary2 = new int[]{10,20,30,40,50}; 16 System.out.println(ary2.length); 17 18 System.out.println(ary2[0]); 19 System.out.println(ary2[4]); 20 21 22 // 【3】數組的字面量聲明 23 int[] ary3 = {10,20,30,40}; 24 25 // 字面量聲明不支持分開賦值 26 /* 27 int[] ary3; 28 ary3 = {10,20,30,40}; 29 */ 30 System.out.println(ary3.length); 31 32 System.out.println(ary3[0]); 33 System.out.println(ary3[3]); 34 35 36 } 37 }?八種常見排序算法---插入算法?
?① 一個數組有序,添加一個元素后,數組依然有序。
1 public class Test07{ 2 public static void main(String[] args){ 3 4 // 一個有序的數組,向該數組中添加一個元素,數組依然有序。 5 int[] ary = {1,3,7,9,12,20,0}; 6 int t = 0; 7 8 // 【1】找位置 9 int loc = -1; // 表示t應該添加到的位置 10 for(int i = 0;i<ary.length-1;i++){ 11 if(ary[i] >= t){ 12 loc = i; 13 break; 14 } 15 } 16 17 System.out.println("loc = "+loc); 18 19 if(loc < 0){ // 沒找到合適的位置 20 arr[ary.length-1] = t; 21 }else{ 22 // 【2】依次后移 23 for(int j=ary.length-1;j>loc;j--){ 24 ary[j] = ary[j-1]; 25 } 26 // 【3】添加插入的值 27 ary[loc] = t; 28 } 29 30 // 驗證 31 for(int i = 0;i<ary.length;i++){ 32 System.out.print(ary[i]+"\t"); 33 } 34 } 35 }?
八種常見排序算法---刪除算法
① 一個有序的數組,刪除一個元素后依然有序。
1 public class Test08{ 2 public static void main(String[] args){ 3 4 // 刪除算法 5 int[] ary = {1,3,7,9,12,20}; 6 int t = 1; 7 8 // 【1】找位置 9 int loc = -1; 10 for(int i=0;i<ary.length;i++){ 11 if(t == ary[i]){ 12 loc = i; 13 break; 14 } 15 } 16 17 // 【2】移動元素 18 if(loc < 0){ 19 System.out.println(t+"在數組中不存在"); 20 }else{ 21 for(int j = loc;j<ary.length-1;j++){ 22 ary[j] = ary[j+1]; 23 } 24 25 // 【3】最后一個元素置0 26 ary[ary.length-1] = 0; 27 } 28 29 // 驗證 30 for(int i = 0;i<ary.length;i++){ 31 System.out.print(ary[i]+"\t"); 32 } 33 34 } 35 }?
?八種常見排序算法---冒泡排序算法
例如:對5,3,4,2,1;數字進行排序
編程思路↓
?示例:
1 public class Test10{ 2 public static void main(String[] args){ 3 // 對一個無序的數組進行排序 4 int[] ary = {10,5,3,4,2,9,7}; 5 6 int tmp = 0; 7 for(int i=0;i<ary.length-1;i++){ // 外層控制趟數 8 9 for(int j=0;j<ary.length-1-i;j++){ // 兩兩比較 10 11 if(ary[j]>ary[j+1]){ 12 tmp = arr[j]; 13 ary[j] = ary[j+1]; 14 ary[j+1] = tmp; 15 } 16 } 17 } 18 19 for(int i=0;i<ary.length;i++){ 20 System.out.print(ary[i]+"\t"); 21 } 22 23 } 24 }?每日一題:
求1到幾的階乘之和;
1 import java.util.Scanner; 2 public class Test01 { 3 public static void main(String[] args){ 4 Scanner sc = new Scanner(System.in); 5 System.out.println("輸入一個數"); 6 int t = sc.nextInt();//控制臺輸入 7 int s =1 ; 8 int sum = 0; 9 10 for(int i=1; i<=t; i++){ 11 s *= i; //輸入的數的階乘 12 sum += s;//從1到輸入的數的階乘之和 13 } 14 System.out.println(t + "的階乘為:"+s); 15 System.out.println("第1 到" + t +"的階乘和為:"+sum); 16 } 17 18 }?
轉載于:https://www.cnblogs.com/abcdjava/p/10720110.html
總結
以上是生活随笔為你收集整理的java数组-如何在一堆数据中使用数组!的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 编程入门:C语言基础知识全网超全不用到处
- 下一篇: react native中一次错误排查