[关闭]
@wangyupu 2020-06-02T08:49:05.000000Z 字数 1992 阅读 29

java基础第六章一维数组及经典应用

java基础


本课目标

  1. 理解数组的作用
  2. 掌握数组的定义
  3. 掌握数组的基本使用及常用操作
  4. 掌握数组常用操作
  5. 获取最大、最小值
  6. 数字排序
  7. 插入数字

为什么需要数组

  1. 数组是一个变量,存储相同数据类型的一组数据
  2. 声明一个变量就是在内存空间划出一块合适的空间
  3. 声明一个数组就是在内存空间划出一串连续的空间

什么是数组

  1. 数组基本要素
  2. 标识符
  3. 数组元素.
  4. 元素下标:从0开始
  5. 元素类型
  6. 数组长度固定不变,避免数组越界

如何使用数组

  1. 使用数组四步走
  2. 1.声明数组
  3. a = new int[5];
  4. 2.分配空间.
  5. a01l=8;
  6. 3.赋值
  7. a [0]= a[0l* 10;
  8. 4.处理数据

声明数组

  1. 声明数组:告诉计算机数据类型是什么
  2. int[ ] score1;
  3. //lava成绩
  4. int score2[ ];
  5. //C#成绩
  6. String[ ] name;
  7. //1学生姓名
  8. 语法
  9. 数据类型数组名[];
  10. 数据类型[]数组名;
  11. 声明数组时不规定数组长度

分配空间

  1. 分配空间:告诉计算机分配几个连续的空间
  2. scores = new int[30];
  3. avgAge = new int[6];
  4. name = new String[30];
  5. 声明数组并分配空间
  6. 数据类型[]数组名= new数据类型[大小] ;
  7. 数组元素根据类型不同,有不同的初始值

数组赋值

  1. 方法1:边声明边赋值
  2. int[ ] scores = {89, 79, 76};
  3. int[ ] scores = new int[ ]{89, 79, 76};
  4. 不能指定数组长度
  5. 方法2:动态地从键盘录入信息并赋值
  6. Scanner input = new Scanner(System.in);
  7. for(int.i=0;i<30;i++){
  8. scores[i] = input.nextlnt();

处理数据

  1. int [] scores = {60, 80, 90, 70, 85};
  2. double avg;
  3. avg = (scores[0] + scores[1] + scores[2] + scores[3] + scores[4])/5;
  4. int [] scores = {60, 80, 90, 70, 85};
  5. int sum= 0;
  6. double avg;
  7. for(int i= 0; i< scores.length; it+){
  8. for(int score:scores){
  9. sum = sum + scores[i];
  10. sum += score;
  11. }
  12. avg = sum/ scores.length;

数组与内存

  1. score = new int[5] ;
  2. 栈内存
  3. 堆内存

求最大值

  1. 根据打擂台的规则
  2. max= stu[O] ;
  3. if (stu[1]>max ){
  4. max=stu [1] ;
  5. if (stu[2]>max ){
  6. max=stu [2] ;
  7. 使用循环来解决
  8. if (stu[3]>max ){
  9. max=stu [3] ;
  10. }
  11. .....

插入数值

  1. 将成绩保存在数组中
  2. 通过比较找到插入位置
  3. 该位置元素往后移一-位
  4. 插入新成绩

老师讲课实录

  1. public class test2 {
  2. /**
  3. * @param args
  4. */
  5. public static void main(String[] args) {
  6. int list[] = {1,9,50,8,9,10,88,99,70,10};
  7. int max = list[0];
  8. for(int i = 0;i<list.length;i++){
  9. if(list[i]>max){
  10. max = list[i];
  11. }
  12. }
  13. System.out.println("max==="+max);
  14. }
  15. }

双重数组示例

  1. import java.util.Scanner;
  2. public class test4 {
  3. public static void main(String[] args) {
  4. String [][] subusers = new String[2][3];
  5. /* subusers[0][0] = "和开箱";
  6. * String [][] s = {{"","",""},{"","",""}};
  7. System.out.println(subusers[1][2]);
  8. */
  9. Scanner sc = new Scanner(System.in);
  10. for(int i = 0;i<subusers.length;i++){
  11. System.out.println("***********************");
  12. System.out.println("开始录入第"+(i+1)+"行学生");
  13. for(int j = 0;j<subusers[i].length;j++){
  14. System.out.println("姓名:");
  15. subusers[i][j] = sc.next();
  16. }
  17. }
  18. System.out.println("-----------------------------");
  19. for(int i = 0;i<subusers.length;i++){
  20. System.out.println("第"+(i+1)+"行学生");
  21. for(int j = 0;j<subusers[i].length;j++){
  22. System.out.println(subusers[i][j]);
  23. }
  24. }
  25. }
  26. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注