• 面向对象综合训练-对象数组的练习


      

    1. package com.luo.demo02;
    2. public class GoodsTest {
    3. public static void main(String[] ags) {
    4. // 1,创建一个数组
    5. Goods[] arr = new Goods[3];
    6. // 2,创建三个商品对象
    7. Goods g1 = new Goods("001","华为p40P",5999,100);
    8. Goods g2 = new Goods("002","保温杯",227,50);
    9. Goods g3 = new Goods("003","枸杞",12.7,70);
    10. //3,把商品添加到对象中
    11. arr[0] = g1;
    12. arr[1] = g2;
    13. arr[2] = g3;
    14. // 4,遍历
    15. for (int i = 0; i < arr.length; i++) {
    16. System.out.println(arr[i].getId() +"," + arr[i].getName()
    17. + "," +arr[i].getPrice() + "," +arr[i].getCount());
    18. }
    19. }
    20. }
    21. package com.luo.demo02;
    22. public class Goods {
    23. private String id;
    24. private String name;
    25. private double price;
    26. private int count;
    27. public Goods() {
    28. }
    29. public Goods(String id,String name,double price,int count) {
    30. this.id = id;
    31. this.name = name;
    32. this.price = price;
    33. this.count = count;
    34. }
    35. public String getId() {
    36. return id;
    37. }
    38. public void setId(String id) {
    39. this.id = id;
    40. }
    41. public String getName() {
    42. return name;
    43. }
    44. public void setName(String name) {
    45. this.name = name;
    46. }
    47. public double getPrice() {
    48. return price;
    49. }
    50. public void setPrice(double price) {
    51. this.price = price;
    52. }
    53. public int getCount() {
    54. return count;
    55. }
    56. public void setCount(int count) {
    57. this.count = count;
    58. }
    59. }

    键盘录入数据 

    1. package com.luo.demo03;
    2. import java.util.Scanner;
    3. public class Test {
    4. public static void main(String[] args) {
    5. /*
    6. 键盘录入
    7. 第一套体系
    8. nextInt(),接收整数
    9. nextDouble(),接收小数
    10. next(),接收字符串
    11. 遇到空格,制表符,回车就停止接受,这些符号后面的数据就不会接受了
    12. 如果录入:123 321 后面如果还有等待接收键盘录入的
    13. 程序也会自动把空格后面的 321 给后面等待接收键盘录入的
    14. 第二套体系
    15. nextLine(),接收字符串
    16. 可以接受空格,制表符,遇到回车才停止接受数据
    17. 键盘录入的两套体系不能混用
    18. 原因:先用 nextInt,再用 nextLine 可能会导致后面的 nextLine 接收不到数据
    19. */
    20. Scanner scanner = new Scanner(System.in);
    21. System.out.println("请输入一个整数");
    22. int num = scanner.nextInt();
    23. System.out.println(num);
    24. System.out.println("请输入一个字符串");
    25. String line = scanner.nextLine();
    26. System.out.println(line);
    27. }
    28. }

     

     键盘录入的两套体系不能混用
     原因:先用 nextInt,再用 nextLine 可能会导致后面的 nextLine 接收不到数据                                 如:输入 123 321,123 被 nextInt 接收了,后面的 空格+321 自动被 nextLine 接收,如果输入的是 123 其实我们输入的是 123+回车,nextInt 只接收了 123,回车还在内存里面,然后回车就会被nextLine 接收了,这种情况下 nextLine 就接收不到数据了

    1. package com.luo.demo03;
    2. import java.util.Scanner;
    3. public class CarTest {
    4. public static void main(String[] args) {
    5. // 1,创建一个数组用来存三个对象
    6. Car[] car = new Car[3];
    7. // 2,创建对象时,数据来自录入
    8. Scanner scanner = new Scanner(System.in);
    9. for (int i = 0; i < car.length; i++) {
    10. System.out.println("请输入第" + (i+1) + "辆汽车的品牌");
    11. String brand = scanner.next();
    12. System.out.println("请输入第" + (i+1) + "辆汽车的价格");
    13. int price = scanner.nextInt();
    14. System.out.println("请输入第" + (i+1) + "辆汽车的颜色");
    15. String color = scanner.next();
    16. Car c = new Car(brand,price,color);
    17. car[i] = c;
    18. // 对象c 给数组传递的也是地址,
    19. // 这个循环一共创建了3 个对象c,分别传递了不同的地址给了数组中的元素
    20. }
    21. // 遍历
    22. for (int i = 0; i < car.length; i++) {
    23. System.out.println(car[i].getBrand() + ","
    24. + car[i].getPrice() + "," + car[i].getColor());
    25. }
    26. }
    27. }
    28. package com.luo.demo03;
    29. public class Car {
    30. private String brand; // 品牌
    31. private int price; // 价格
    32. private String color; // 颜色
    33. public Car() {
    34. }
    35. public Car(String brand, int price, String color) {
    36. this.brand = brand;
    37. this.price = price;
    38. this.color = color;
    39. }
    40. public String getBrand() {
    41. return brand;
    42. }
    43. public void setBrand(String brand) {
    44. this.brand = brand;
    45. }
    46. public int getPrice() {
    47. return price;
    48. }
    49. public void setPrice(int price) {
    50. this.price = price;
    51. }
    52. public String getColor() {
    53. return color;
    54. }
    55. public void setColor(String color) {
    56. this.color = color;
    57. }
    58. }

    1. package com.luo.demo04;
    2. import org.jetbrains.annotations.NotNull;
    3. public class Test {
    4. public static void main(String[] args) {
    5. // 创建数组用来储存学生类
    6. Student[] arr = new Student[3];
    7. // 创建学生对象添加到数组中
    8. Student student1 = new Student(1,"罗",18);
    9. Student student2 = new Student(2,"惠",19);
    10. Student student3 = new Student(3,"祥",20);
    11. // 把学生对象添加到数组中
    12. arr[0] = student1;
    13. arr[1] = student2;
    14. arr[2] = student3;
    15. // 要求1,再次添加一个学生对象,并在添加的时候进行学号的判断是否唯一
    16. Student student4 = new Student(4,"张三",21);
    17. // 唯一性判断
    18. boolean flag = contains(arr, student4.getId());
    19. if (flag) {
    20. // 已经存在了,不需要添加
    21. System.out.println("当前id重复,请修改id");
    22. }else if(getCount(arr) == arr.length){
    23. // 数组已经存满了
    24. // 只能创建一个新数组,新数组长度等于老数组长度加一
    25. Student[] newArr = creatNewArr(arr);
    26. newArr[getCount(arr)] = student4;
    27. printArr(newArr);
    28. }else {
    29. // 数组没有存满,我们也创建一个新数组
    30. arr[getCount(arr)] = student4;
    31. printArr(arr);
    32. }
    33. System.out.println("==================");
    34. // 要求3,通过id删除学生信息
    35. delete(arr,2);
    36. // 遍历学生信息
    37. printArr(arr);
    38. System.out.println("==================");
    39. // 要求4,通过id给学生年龄+1
    40. modifyAge(arr,3);
    41. // 遍历学生信息
    42. printArr(arr);
    43. }
    44. // id进行唯一性判断的方法
    45. public static boolean contains(Student[] arr, int id) {
    46. for (int i = 0; i < arr.length; i++) {
    47. if (arr[i] != null) {
    48. // 如果arr[i]的null,null不能直接使用,所以前面还需要加个判断
    49. if (arr[i].getId() == id) {
    50. return true;
    51. }
    52. }
    53. }
    54. return false;
    55. }
    56. // 求数组中存在了几个元素的方法,
    57. // 同时求从数组中第几个元素开始没有赋值的方法
    58. public static int getCount(Student[] arr) {
    59. int count = 0;
    60. for (int i = 0; i < arr.length; i++) {
    61. if (arr[i] != null) {
    62. count++;
    63. }
    64. }
    65. return count;
    66. }
    67. // 创建一个新数组,把老数组存入到新数组中
    68. public static Student[] creatNewArr(Student[] arr) {
    69. Student[] newArr = new Student[arr.length + 1];
    70. for (int i = 0; i < getCount(arr); i++) {
    71. newArr[i] = arr[i];
    72. }
    73. return newArr;
    74. }
    75. // 遍历数组的方法
    76. public static void printArr(Student[] students) {
    77. for (int i = 0; i < students.length; i++) {
    78. Student student = students[i];
    79. if (student != null) {
    80. System.out.println(students[i].getId() + ","
    81. + students[i].getName() + "," + students[i].getAge());
    82. }
    83. }
    84. }
    85. //通过id查找学生的方法
    86. public static int getId(Student[] arr,int id) {
    87. for (int i = 0; i < arr.length; i++) {
    88. if (arr[i] != null) {
    89. if (arr[i].getId() == id) {
    90. return i;
    91. }
    92. }
    93. }
    94. System.out.println("没有此id");
    95. return -1;
    96. }
    97. // 通过id删除学生信息
    98. public static void delete(Student[] arr,int id) {
    99. if (getId(arr,id) == -1) {
    100. }else {
    101. arr[getId(arr,id)] = null;
    102. }
    103. }
    104. // 通过id将学生的年龄+1
    105. public static void modifyAge(Student[] arr,int id) {
    106. if (getId(arr,id) == -1) {
    107. }else {
    108. arr[getId(arr,id)].setAge(arr[getId(arr, id)].getAge() + 1);
    109. }
    110. }
    111. }
    112. package com.luo.demo04;
    113. public class Student {
    114. private int id;
    115. private String name;
    116. private int age;
    117. public Student() {
    118. }
    119. public Student(int id, String name, int age) {
    120. this.id = id;
    121. this.name = name;
    122. this.age = age;
    123. }
    124. public int getId() {
    125. return id;
    126. }
    127. public void setId(int id) {
    128. this.id = id;
    129. }
    130. public String getName() {
    131. return name;
    132. }
    133. public void setName(String name) {
    134. this.name = name;
    135. }
    136. public int getAge() {
    137. return age;
    138. }
    139. public void setAge(int age) {
    140. this.age = age;
    141. }
    142. }

     

  • 相关阅读:
    MYSQL 同步到ES 如何设计架构保持一致性
    【机器学习项目实战10例】(四):利用XGBoost实现短期电力负荷预测
    图的存储结构--邻接矩阵
    HTML实现轮播图
    Redis那些事儿(一)
    图片上怎么加文字?看完就你知道了
    SQL注入漏洞(绕过篇)
    电力通信规约CDT/Modbus/101/103/104/DL/T645应用分析
    带你深度解析虚幻引擎4的照明和阴影知识
    教你遇到vcomp120.dll无法继续执行代码的解决方法
  • 原文地址:https://blog.csdn.net/Lhx2046933776/article/details/128177108