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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值