Java SE入门及基础(24)

发布时间:2024年01月21日

目录

方法带参(续第23篇文章)

3. 对象数组

案例场景

练习

4. 引用数据类型作为方法的参数

案例场景

分析

代码实现

5. 数组作为方法的参数

案例场景

Java?SE文章参考:Java SE入门及基础知识合集-CSDN博客


方法带参(续第23篇文章)

3. 对象数组

案例场景

学生有姓名和年龄,类定义如下:
public class Student {
????????public String name ;
????????public int age ;
????????public Student ( String name , int age ){
????????????????this . name = name ;
????????????????this . age = age ;
????????}
}
一个班级有多个学生,如何存储这些学生的信息?使用数组存储
public class StudentTest {
????????public static void main ( String [] args ) {
????????????????int [] numbers = new int [ 2 ];
????????????????numbers [ 0 ] = 10 ;
????????????????Student [] students = new Student [ 2 ];
????????????????students [ 0 ] = new Student ( " 张三 " , 20 );
????????????????students [ 1 ] = new Student ( " 李四 " , 25 );
????????}
}

练习

使用对象数组存储学生选择的 5 门必修课程(课程编号,课程名称,学分)
public class Course {
????????public String number ; // 课程编号
????????public String name ; // 课程名称
????????public double score ; // 学分
????????public Course ( String number , String name , double score ){
????????????????this . number = number ;
????????????????this . name = name ;
????????????????this . score = score ;
????????}
}
public class CourseTest {
????????public static void main ( String [] args ) {
????????????????Course [] courses = new Course [ 5 ];
????????????????courses [ 0 ] = new Course ( "C0001" , "Java" , 5 );
????????????????courses [ 1 ] = new Course ( "C0002" , "JDBC" , 2 );
????????????????courses [ 2 ] = new Course ( "C0003" , "Html" , 3 );
????????????????courses [ 3 ] = new Course ( "C0004" , "Jsp" , 6 );
????????????????courses [ 4 ] = new Course ( "C0005" , "Spring" , 10 );
????????}
}

4. 引用数据类型作为方法的参数

案例场景

????????某手机专卖店有100 个手机展架,售货员现在依次向展架上摆放手机。请使用面向对象的设计思想描述这一过程。(手机有品牌,型号和价格)

分析

????????a. 这一过程设计到的对象有两个,一个是手机,一个是售货员。因此我们需要为这两个对象构建类
????????b. 摆放手机是售货员的一个行为,因此需要使用方法来描述
????????c. 100个手机展架放的都是手机,因此需要使用对象数组来存储

代码实现

public class Mobile {
????????public String brand ;
????????public String type ;
????????public double price ;
????????public Mobile ( String brand , String type , double price ){
????????????????this . brand = brand ;
????????????????this . type = type ;
????????????????this . price = price ;
????????}
}
public class Seller {
????????//数组中的默认值都是 null
????????public Mobile [] mobiles = new Mobile [ 100 ];
/**
* 引用数据类型作为方法的参数
* @param mobile
*/
????????public void playMobile ( Mobile mobile ){
????????????????for ( int i = 0 ; i < mobiles . length ; i ++ ){
????????????????????????if ( mobiles [ i ] == null ){
????????????????????????????????mobiles [ i ] = mobile ;
????????????????????????????????break ;
????????????????????????}
????????????????}
????????}
}
public class SellerTest {
????????public static void main ( String [] args ) {
????????????????Seller seller = new Seller ();
????????????????//调用售货员放手机
????????????????seller . playMobile ( new Mobile ( " 小米 " , " 小米 10" , 2000 ));
????????}
}

5. 数组作为方法的参数

案例场景

现有甲乙丙三个班级成绩统计如下:
甲: 80,72,85,67,50,76,95,49
乙: 77,90,92,89,67,94
丙: 99,87,95,93,88,78,85
现要求将每个班的成绩从高到低依次排列。
public class ArraySort {
????????public static void main ( String [] args ) {
????????????????int [] arr1 = { 80 , 72 , 85 , 67 , 50 , 76 , 95 , 49 };
????????????????int [] arr2 = { 77 , 90 , 92 , 89 , 67 , 94 };
????????????????int [] arr3 = { 99 , 87 , 95 , 93 , 88 , 78 , 85 };
????????????????sortDesc ( arr1 );
????????????????System . out . println ( Arrays . toString ( arr1 ));
????????????????sortDesc ( arr2 );
????????????????System . out . println ( Arrays . toString ( arr2 ));
????????????????sortDesc ( arr3 );
????????????????System . out . println ( Arrays . toString ( arr3 ));
}
public static void sortDesc ( int [] arr ){
????????//可以使用冒泡排序来对数组中的元素进行降序排列
????????for ( int i = 0 ; i < arr . length ; i ++ ){
????????????????for ( int j = 0 ; j < arr . length - i - 1 ; j ++ ){
????????????????????????if ( arr [ j ] < arr [ j + 1 ]){
????????????????????????????????int temp = arr [ j ];
????????????????????????????????arr [ j ] = arr [ j + 1 ];
????????????????????????????????arr [ j + 1 ] = temp ;
????????????????????????}
????????????????}
????????}
}
}



Java
?SE文章参考:Java SE入门及基础知识合集-CSDN博客

文章来源:https://blog.csdn.net/m0_63181360/article/details/135723723
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。