• 从零开始学JAVA(04):数组


    一、数组的特点

    • 数组是相同类型的变量的集合,所有元素的类型都一样
    • 可以指定数组包含的元素个数,最多为int的最大值个
    • 元素有固定的顺序
    • 每个元素都有一个固定的编号,称之为索引(index),从0开始递增,类型为int
    • 可以像操作变量一样读写数组中的任何一个元素

    二、创建和使用一个数组的语法

    • 数组元素类型[] 变量名 = new 数组元素类型[数组长度]
    • 变量名[索引] 可以使用这个变量,可以读取也可以给它赋值

    三、数组实践

    1. public class CreateArray {
    2. public static void main(String[] args) {
    3. int[] intArray = new int[100]; // 长度为100的数组
    4. System.out.println(intArray[10]); // 如果没有赋值,默认值为0
    5. double[] doubleArray = new double[100];
    6. System.out.println(doubleArray[20]); // 如果没有赋值,默认值为0.0
    7. intArray[20] = 100;
    8. System.out.println(intArray[20]);
    9. // 数组越界
    10. // 报错:Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 101 out of bounds for length 100
    11. System.out.println(intArray[101]);
    12. }
    13. }

    四、数组的名与实

    • 数组的“实”是一块地址连续的内存,就像是编号连续的白纸一样
    • 数组的名,就是这个块连续内存的第一个内存的地址
    • 数据的变量和基本变量一样,本身是个地址,但是与变量不一样的是,这个地址的值,是数组的”名“,也就是数组的第一个地址
    • 数组 = 数组变量+数组的实体
    • 数组变量[索引]就是在数组原有地址的基础上,加上索引,获得想要的元素
    • 所以索引是从0开始的,因为数组变量的地址就是数组第一个元素的地址

    五、数组的长度

    • 数组变量.length可以获得数组的长度
    • 与JavaScript不同的是,java中的数组一旦创建,其长度是不可变的
    • 数组越界的错误是ArrayIndexOutOfBoundsException
    • 数组里每个元素都有初始值,初始值和类型有关。对于int,初始值为0;对于Boolean,初始值为false
    • 数组变量可以指向新的数组实体。这时候,数组变量的值就是新的数组实体的地址了
    • 如果没有别的数组变量指向原来数组实体,原来的数组实体也就不可访问了
    1. public class CreateArray {
    2. public static void main(String[] args) {
    3. int[] intArray = new int[100]; // 长度为100的数组
    4. System.out.println(intArray.length);
    5. }
    6. }
    1. public class CreateArray {
    2. public static void main(String[] args) {
    3. int[] intArray = new int[100]; // 长度为100的数组
    4. System.out.println(intArray.length); // 100
    5. intArray = new int[10];
    6. System.out.println(intArray.length); // 10
    7. }
    8. }

    六、二维数组

    • 二维数组是一维数组的延伸
    • double[][] double2Array = new double[5][10]
    1. public class CreateArray {
    2. public static void main(String[] args) {
    3. double[][] double2Array = new double[5][3];
    4. for (int i = 0; i < double2Array.length; i++) {
    5. for (int j = 0; j < double2Array[i].length; j++) {
    6. double2Array[i][j] = Math.random() * 100;
    7. System.out.println(double2Array[i][j]);
    8. }
    9. }
    10. }
    11. }

    七、一个程序

    1. import java.util.Scanner;
    2. public class ScoreMaster {
    3. public static void main(String[] args) {
    4. // 声明六个变量, 分别代表六门科目的成绩
    5. int YuWenIndex = 0;
    6. int ShuXueIndex = 1;
    7. int WaiYuIndex = 2;
    8. int WuLiIndex = 3;
    9. int HuaXueIndex = 4;
    10. int ShengWuIndex = 5;
    11. int totalScoreCount = 6;
    12. // 每门课的名字
    13. String[] names = new String[totalScoreCount];
    14. names[YuWenIndex] = "语文";
    15. names[ShuXueIndex] = "数学";
    16. names[WaiYuIndex] = "外语";
    17. names[WuLiIndex] = "物理";
    18. names[HuaXueIndex] = "化学";
    19. names[ShengWuIndex] = "生物";
    20. Scanner scanner = new Scanner(System.in);
    21. System.out.println("请输入共有多少年的成绩:");
    22. int yearCount = scanner.nextInt();
    23. double[][] scores = new double[yearCount][totalScoreCount];
    24. for (int i = 0; i < yearCount; i++) {
    25. for (int j = 0; j < totalScoreCount; j++) {
    26. scores[i][j] = 80 + Math.random() * 20;
    27. System.out.println("第" + (i + 1) + "年" + names[j] + "成绩为:" + scores[i][j]);
    28. }
    29. }
    30. boolean cont = true;
    31. while (cont) {
    32. System.out.println("请选择要进行的操作:");
    33. System.out.println("1: 求某年最好成绩\n" +
    34. "2: 求某年的平均成绩\n" +
    35. "3: 求所有年份最好成绩\n" +
    36. "4: 求某门课历年最好成绩");
    37. int oprtId = scanner.nextInt();
    38. int year = 0;
    39. switch (oprtId) {
    40. case 1:
    41. // 让用户输入指定的年份
    42. System.out.println("请输入要计算第几年的最好成绩");
    43. year = scanner.nextInt();
    44. if (year <= 0 || yearCount < year) {
    45. System.out.println("非法的年份:" + year);
    46. cont = false;
    47. break;
    48. }
    49. year = year - 1;
    50. // 指定年份的最好成绩的编号,开始假设是0
    51. int bestOfYearScoreId = 0;
    52. // 循环指定年份的成绩,找出最好的成绩
    53. // TODO:如果有两门课的成绩一样,而且都是最高的,怎么办?
    54. for (int i = 1; i < totalScoreCount; i++) {
    55. if (scores[year][bestOfYearScoreId] < scores[year][i]) {
    56. bestOfYearScoreId = i;
    57. }
    58. }
    59. System.out.println("第" + (year + 1) + "年成绩最好的科目为" + names[bestOfYearScoreId] + ",成绩为" + scores[year][bestOfYearScoreId] + "。");
    60. break;
    61. case 2:
    62. System.out.println("请输入要计算第几年的平均成绩");
    63. year = scanner.nextInt();
    64. if (year <= 0 || yearCount < year) {
    65. System.out.println("非法的年份:" + year);
    66. cont = false;
    67. break;
    68. }
    69. year = year - 1;
    70. double totalCountForAvg = 0;
    71. for (int i = 0; i < totalScoreCount; i++) {
    72. totalCountForAvg += scores[year][i];
    73. }
    74. double avgOfYear = totalCountForAvg / totalScoreCount;
    75. System.out.println("第" + (year + 1) + "年的平均成绩为" + avgOfYear + "。");
    76. break;
    77. case 3:
    78. int bestYear = 0;
    79. int bestScore = 0;
    80. for (int i = 0; i < yearCount; i++) {
    81. for (int j = 0; j < totalScoreCount; j++) {
    82. if (scores[bestYear][bestScore] < scores[i][j]) {
    83. bestYear = i;
    84. bestScore = j;
    85. }
    86. }
    87. }
    88. // 视频中代码有错误,应该是使用 bestYear 而不是 year
    89. System.out.println("所有年度最好成绩为第" + (bestYear + 1) + "年的" + names[bestScore] + ",成绩为" + scores[bestYear][bestScore] + "。");
    90. break;
    91. case 4:
    92. System.out.println("请输入科目编号");
    93. int subjectId = scanner.nextInt();
    94. if (subjectId <= 0 || totalScoreCount < subjectId) {
    95. System.out.println("非法的科目编号:" + subjectId);
    96. cont = false;
    97. break;
    98. }
    99. subjectId = subjectId - 1;
    100. year = 0;
    101. for (int i = 1; i < yearCount; i++) {
    102. if (scores[year][subjectId] < scores[i][subjectId]) {
    103. year = i;
    104. }
    105. }
    106. System.out.println("第" + (year + 1) + "年度" + names[subjectId] + "成绩最好,为" + scores[year][subjectId] + "。");
    107. break;
    108. default:
    109. cont = false;
    110. System.out.println("不支持:" + oprtId + ", 程序结束。");
    111. }
    112. }
    113. }
    114. }

  • 相关阅读:
    数据结构───顺序表(梦开始的地方)
    真的牛!简历这样写,面试电话约面不断!!
    爱创科技携手洽洽食品,探索渠道数字化最优解!
    如何理解分类任务中的logits?
    编译libigl笔记
    Java 8 Stream API可以怎么玩?
    ChatGPT科研绘图丨散点图、柱状图、小提琴图、箱型图、雷达图、玫瑰图、气泡图、森林图、三元图、三维图等各类科研图
    JS力扣第九题-回文数
    Java网络教程之URL及URLConnection简介说明
    Python-Numpy中的repmat
  • 原文地址:https://blog.csdn.net/m0_47135993/article/details/128088038