随笔 - 86  文章 - 5  评论 - 11  阅读 - 10万 

声明一个变量就是在内存空间划出一块合适的空间。声明一个数组就是在内存空间划出一串连续的空间。

数组基本要素:

  标识符:数组的名称,用于区分不同的数组

  数组元素:向数组中存放的数据

  元素下标:对数组元素进行编号,从0开始,数组中的每个元素都可以通过下标来访问

  元素类型:数组元素的数据类型

语法:数据类型    数组名[ ]   或  数据类型[ ]  数组名 ;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
        //声明数组
int [] score ;
int score2[];
//分配空间
score= new int[3];
score2= new int[6];
//赋值
score[2] = 48;
score[0] = 90;
score[1] = 67;
//边声明边赋值
int[] score3 = {55,78,90};
int[] score4 = new int[] {88,33,77};       
//边声明边分配
int[] score5 = new int[3];             
System.out.println((score[0]+score[1]+score[2])/3);
 
/*
 * 数组应用
 */
double [] list = {89.5,57,98,43.5};
double max = list[0];
double total = 0;
for(int i =0;i<list.length;i++) {
    //打印所有数组元素
    System.out.print(list[i]+"\t");
}
for(int i=0;i<list.length;i++) {
    total = list[i]+total;         
}
System.out.println("所有元素之和是:"+total);
for(int i =0;i<list.length;i++) {
    if(list[i]>max) {
        max = list[i];
    }
}
System.out.println("最大元素是:"+max);

  

posted on   史振兴  阅读(302)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示