博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java数组-如何在一堆数据中使用数组!
阅读量:6989 次
发布时间:2019-06-27

本文共 4299 字,大约阅读时间需要 14 分钟。

数组

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
= 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

 

八种常见排序算法---删除算法

① 一个有序的数组,删除一个元素后依然有序。

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

 

 八种常见排序算法---冒泡排序算法

例如:对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[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

 每日一题:

求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

你可能感兴趣的文章
android非法字符的判定、表情符号的判定
查看>>
为了眼睛的健康改变电脑的色调-献给长期坐在电脑前的朋友
查看>>
How to enable LDAP over SSL with a third-party certification authority
查看>>
细说 Data URI
查看>>
SqlServer 查询死锁,杀死死锁进程
查看>>
SNS商业
查看>>
利用ICSharpCode.SharpZipLib.dll实现简单加解压 转
查看>>
今天才知道什么是欲哭无泪
查看>>
pgpool 后台运行方法
查看>>
精品素材:推荐15套非常漂亮的 iOS 图标素材
查看>>
wpf之ComboBox绑定
查看>>
使用HttpSessionListener接口监听Session的创建和失效
查看>>
应用更新iOS 开发:应用内实现 更新提醒
查看>>
Android ToggleButton Example--开关按钮
查看>>
android 国际化
查看>>
hdu1503
查看>>
10000单词积累,从今天开始(待续)。。。
查看>>
转Spring+Hibernate+EHcache配置(三)
查看>>
显示系统托盘列表(并执行隐藏与显示)
查看>>
Source Insight 使用
查看>>