null JAVA数组 JAVA数组目的目的数组的定义 规范
数组的使用
数组的优缺点null 1、一次Java内部测试结束后,老师给令狐冲分配了一项任务,计算全班(30人)的平均分为什么需要数组int stu1 = 95;
int stu2 = 89;
int stu3 = 79;
int stu4 = 64;
int stu5 = 76;
int stu6 = 88;
……
avg = (stu1+stu2+stu3+stu4+stu5…+stu30)/30
30个变量太繁琐null生活案例:博物架
为什么需要数组字画类好办法——分类放,易于找
1、格子提供了存储空间
2、每一类别都起一个名字
3、每件物品都有个标号古玩类null类比博物架:可不可以把数据归类存放?
分类存放不同类型的数据
int类型double类型char类型数组数组—数组在内存中的存储数组—数组在内存中的存储80内存整型数组
int[ ] b = {100,98,…}; 整型变量
int a = 80; 数组定义数组定义数组是一个变量,存储相同数据类型的一组数据int类型元素 博物架名
符 古玩 物品编号 元素下标 物品类型元素类型 数据 小结小结
2、下列哪组数据能存储在数组中?数组的类型是什么?
“刘星”,“夏雨”,“夏雪”
8,98,‘c’,23
98.1,341.2,34.3
数组中的所有元素必须属于相同的数据类型 String型 double型 如何使用数组使用数组四步走:
1、声明数组
2、分配空间
3、赋值
4、处理数据
如何使用数组int[ ] a; a = new int[5]; a [0] = 8; a [0] = a[0] * 10; a880a[0] 数组的声明 数组的声明int[ ] score1; //Java成绩
int score2[ ]; //C#成绩
String[ ] name; //学生姓名声明数组: 告诉计算机数据类型是什么1数据类型 数组名[ ] ; 数据类型[ ] 数组名 ; 数组初始化 数组初始化score = new int[30];
avgAge = new int[6];
name = new String[30];30……分配空间: 告诉计算机分配几个格子数组属于引用数据类型 2数据类型[ ] 数组名 = new 数据类型[大小] ; 声明数组并分配空间 数组赋值 数组赋值score[0] = 89;
score[1] = 79;
score[2] = 76;
……赋值:向分配的格子里放数据……30score[0]score[1]score[2] 89 79 76太麻烦!能不能一起赋值?3数组赋值数组赋值方法1: 边声明边赋值
方法2:动态地从键盘录入信息并赋值解决int[ ] score = {89, 79, 76};Scanner input = new Scanner(System.in); //获取控制台输入信息
for(int i = 0; i < 30; i ++){
score[i] = input.nextInt(); //将控制台输入的数据转为int类型
}int[ ] score = new int[ ]{89, 79, 76}; 使用数组求平均值 使用数组求平均值int [ ] score = {60, 80, 90, 70, 85};
double avg;
avg = (score[0] + score[1] + score[2] + score[3] + score[4])/5; int [ ] score = {60, 80, 90, 70, 85};
int sum = 0;
double avg;
for(int index = 0; index < score.length; index++){
sum = sum + score[index];
}
avg = sum / score.length; 成绩单访问数组成员:使用“标识符[下标]”下标数组的length属性对数据进行处理:计算5位学生的平均分4使用数组求平均值使用数组求平均值回顾问题1: 令狐冲的班里有30位学生,使用动态录入并赋值的方式计算平均分
常见错误public class Hello {
public static void main(String[ ] args){
int[ ] score = new int[ ];
score[0] = 89;
score[1] = 63;
System.out.println(score[0]);
}
} 常见错误编译出错,没有写明数组的大小 常见错误public class Hello {
public static void main(String[ ] args){
int[ ] score = new int[2];
score[0] = 89;
score[1] = 63;
score[2] = 45;
System.out.println(score[2]);
}
} 常见错误编译出错,数组越界常见错误常见错误public static void main(String[ ] args){
int[ ] score = new int[5];
score = {60, 80, 90, 70, 85};
int[ ] score2;
score2 = {60, 80, 90, 70, 85};
} 编译出错,创建数组并赋值的方式必须在一条语句中完成 public class Hello {
public static void main(String[ ] args){
int[ ] score = new int[3];
score[0] = 89;
System.out.println(score[1]);
}
} 0输出结果 小结 小结
使用数组的步骤是什么?
有一个数列:8,4,2,1,23,344,12
1)循环输出数列的值
2)求数列中所有数值的和
3)猜数游戏:从键盘中任意输入一个数据,判断数列中是否包含此数
求最大值 2、从键盘输入本次Java考试五位学生的成绩,求考试成绩最高分
求最大值 求最大值 求最大值根据打擂台的规则:max = stu[0] ;
if (a[1]>max ){
max=a[1] ;
}
if (a[2]>max ){
max=a[2] ;
}
if (a[3]>max ){
max=a[3] ;
}
……使用循环
来解决求最大值求最大值 //计算成绩最大值
max = score[0];
for (int index = 1;index < 5;index++) {
if (score[index] > max) {
max = score[index];
}
}
System.out.println("本次考试的五位学生的最高分是: " + max);循环打擂使用max存储擂主初始值:第一个元素为擂主小结小结 小明要去买一部手机,他询问了4家店的价格,分别是2800元,2900元,2750元和3100元,显示输出最低价 int[] list=new int[]{2800,2900,2750,3100};
int min=0;
for(int i=0;i
list[i])
{
min=list[i];
}
}
}
System.out.println(min);小结4小结4 有一列乱序的字符,‘a’,‘c’,‘u’,‘b’,‘e’,‘p’,‘f’,‘z’,排序并按照英文字母的逆序输出char list[ ] = {'a','c','u','b','e','p','f','z'};
Arrays.sort(list);
for (int i = list.length - 1; i >= 0; i-- ) {
System.out.println(list[i]);
}排序排序循环录入5位学员成绩,进行升序排列后输出结果
5位同学的成绩分别为:90,80,92,81,78 提示:使用冒泡排序法删除删除有一个数组记录了6个学生的成绩,现在有个学生已经转校了,那么要删除这个学生的成绩怎么办? int[] list=new int[]{80,90,70,81,67,72};
//删除第3个元素
int [] newlist=new int[5];
int k=0;
for(int i=0;i