1 /**
2 *
3 * @Description
4 * @author Bytezero·zhenglei! Email:420498246@qq.com
5 * @version
6 * @date 2021年9月12日上午9:06:47
7 * @
8 * 对象数组题目
9 * 定义类:Student,包含三个属性:学号number(int),年级state(int),
10 * 成绩score(int)
11 * 创建20个学生对象,学号为1-20,年级和成绩都有随机数确定
12 * 问题一:打印出3年级(state值为3)的学生信息
13 * 问题二:使用冒泡排序按学生成绩排序,并遍历所有学生信息
14 *
15 * 提示:生成随机数 Math.random(),返回值double
16 * 四舍五入取整数:Math.round(double d),返回值类型long
17 */
18 public class StudentTest
19 {
20 public static void main(String[] args)
21 {
22 //声明Student类型的数组
23 Student[] stus = new Student[20];
24 for(int i =0; i <stus.length;i++)
25 {
26 //给数组元素赋值
27 stus[i] = new Student();
28 //给student对象的属性赋值
29 stus[i].number= (i+1);
30 //要求年级[1-6]
31 stus[i].state =(int)(Math.random()*(6-1+1)+1);
32 //成绩 [0-100]
33 stus[i].score = (int)(Math.random()*(100-0+1)+0);
34 }
35
36 //遍历学生数组
37 for(int i =0; i <stus.length; i++)
38 {
39 //System.out.println(stus[i].number+"\t"+stus[i].score+"\t"+stus[i].state);
40
41 System.out.println(stus[i].info());
42 }
43
44 System.out.println("******************************************");
45 //问题一:打印出3年级(state值为3)的学生信息
46 for(int i = 0; i <stus.length;i++)
47 {
48 if(stus[i].state==3)
49 {
50 System.out.println(stus[i].info());
51 }
52
53 }
54
55 //问题二:使用冒泡排序按学生成绩排序,并遍历所有学生信息
56 System.out.println("******************************************");
57 System.out.println( "问题二:使用冒泡排序按学生成绩排序,并遍历所有学生信息");
58
59 for(int i =0;i <stus.length-1;i++)
60 {
61 for(int j =0;j<stus.length-1-i;j++)
62 {
63 if(stus[j].score>stus[j+1].score)
64 {
65 //交换的是数组的元素:Student对象!!!
66 Student temp = stus[j];
67 stus[j] = stus[j+1];
68 stus[j+1] = temp;
69 }
70 }
71 }
72
73 //遍历学生数组
74 for(int i =0; i <stus.length; i++)
75 {
76 //System.out.println(stus[i].number+"\t"+stus[i].score+"\t"+stus[i].state);
77
78 System.out.println(stus[i].info());
79 }
80
81
82 }
83 }
84
85 class Student
86 {
87 int number; //学号
88 int state; //年级
89 int score; //成绩
90
91
92 //显示学生信息的方法
93 public String info()
94 {
95 return"学号:"+number+"\t年级:"+state+"\t成绩:"+score;
96 }
97
98 }
99
100
101
102 /****************************改进**************************/
103
104 /**
105 *
106 * @Description
107 * @author Bytezero·zhenglei! Email:420498246@qq.com
108 * @version
109 * @date 2021年9月12日上午9:06:47
110 * @
111 * 对象数组题目
112 * 定义类:Student,包含三个属性:学号number(int),年级state(int),
113 * 成绩score(int)
114 * 创建20个学生对象,学号为1-20,年级和成绩都有随机数确定
115 * 问题一:打印出3年级(state值为3)的学生信息
116 * 问题二:使用冒泡排序按学生成绩排序,并遍历所有学生信息
117 *
118 * 提示:生成随机数 Math.random(),返回值double
119 * 四舍五入取整数:Math.round(double d),返回值类型long
120 */
121
122 /*********** 改进 **********/
123 /*将操作数组的功能封装到方法中*/
124
125
126
127 public class StudentTest2
128 {
129 public static void main(String[] args)
130 {
131 //声明Student类型的数组
132 Student2[] stus = new Student2[20];
133 for(int i =0; i <stus.length;i++)
134 {
135 //给数组元素赋值
136 stus[i] = new Student2();
137 //给student对象的属性赋值
138 stus[i].number= (i+1);
139 //要求年级[1-6]
140 stus[i].state =(int)(Math.random()*(6-1+1)+1);
141 //成绩 [0-100]
142 stus[i].score = (int)(Math.random()*(100-0+1)+0);
143 }
144
145 StudentTest2 test = new StudentTest2();
146 //遍历学生数组
147 test.print(stus);
148
149 //问题一:打印出3年级(state值为3)的学生信息
150 System.out.println("******************************************");
151 System.out.println("问题一:打印出3年级(state值为3)的学生信息");
152 test.serchState(stus, 3);
153
154
155 //问题二:使用冒泡排序按学生成绩排序,并遍历所有学生信息
156 System.out.println("******************************************");
157 System.out.println( "问题二:使用冒泡排序按学生成绩排序,并遍历所有学生信息");
158 test.sort(stus);
159 //遍历学生数组
160 test.print(stus);
161
162 }
163
164 /**
165 *
166 * @Description 遍历Student2[]数组的操作
167 * @author Bytezero·zhenglei!
168 * @date 2021年9月12日上午9:56:45
169 * @param stus
170 *
171 */
172 public void print(Student2[] stus)
173 {
174 //遍历学生数组
175 for(int i =0; i <stus.length; i++)
176 {
177 //System.out.println(stus[i].number+"\t"+stus[i].score+"\t"+stus[i].state);
178
179 System.out.println(stus[i].info());
180 }
181 }
182
183 /**
184 *
185 * @Description 查找Student数组中指定的学生信息
186 * @author Bytezero·zhenglei!
187 * @date 2021年9月12日上午9:52:52
188 * @param stus 要查找的数组
189 * @param state 要找的年级
190 *
191 */
192 public void serchState(Student2[] stus,int state)
193 {
194 //问题一:打印出3年级(state值为3)的学生信息
195 for(int i = 0; i <stus.length;i++)
196 {
197 if(stus[i].state==state)
198 {
199 System.out.println(stus[i].info());
200 }
201
202 }
203 }
204
205 /**
206 *
207 * @Description 给Student2数组排序
208 * @author Bytezero·zhenglei!
209 * @date 2021年9月12日上午9:55:35
210 * @param stus
211 *
212 */
213 public void sort(Student2[] stus)
214 {
215 for(int i =0;i <stus.length-1;i++)
216 {
217 for(int j =0;j<stus.length-1-i;j++)
218 {
219 if(stus[j].score>stus[j+1].score)
220 {
221 //交换的是数组的元素:Student对象!!!
222 Student2 temp = stus[j];
223 stus[j] = stus[j+1];
224 stus[j+1] = temp;
225 }
226 }
227 }
228 }
229
230
231
232
233 }
234
235 class Student2
236 {
237 int number; //学号
238 int state; //年级
239 int score; //成绩
240
241
242 //显示学生信息的方法
243 public String info()
244 {
245 return"学号:"+number+"\t年级:"+state+"\t成绩:"+score;
246 }
247
248 }

Java 对象数组题目 + 改进(封装方法)的更多相关文章

  1. JSon_零基础_007_将JSon格式的"数组"字符串转换为Java对象"数组"

    将JSon格式的"数组"字符串转换为Java对象"数组". 应用此技术从一个json对象字符串格式中得到一个java对应的对象. JSONObject是一个“n ...

  2. java对象数组

    问题描述:     java 对象数组的使用 问题解决: 数组元素可以是任何类型(只要所有元素具有相同的类型) 数组元素可以是基本数据类型 数组元素也可以是类对象,称这样的数组为对象数组.在这种情况下 ...

  3. 将java中数组转换为ArrayList的方法实例(包括ArrayList转数组)

    方法一:使用Arrays.asList()方法   1 2 String[] asset = {"equity", "stocks", "gold&q ...

  4. java 对象数组

    java 对象数组 from zhaocundang@163.com 先 用类声明数组: 再把类的实例赋给数组: package works; import java.util.Scanner; pu ...

  5. java中数组有没有length()方法?string没有lenght()方法?

    java中数组有没有length()方法,求数组的长度可以使用数组的length属性. int[] arr={1,2,3,4,5}; int length=arr.length;//求数组的长度 -- ...

  6. 【Java面试题】18 java中数组有没有length()方法?string没有lenght()方法?下面这条语句一共创建了多少个对象:String s="a"+"b"+"c"+"d";

    数组没有length()这个方法,有length的属性.String有有length()这个方法. int a[]; a.length;//返回a的长度 String s; s.length();// ...

  7. java对象数组的概述和使用

    1 public class Student 2 { 3 // 成员变量 4 private String name; 5 private int age; 6 7 // 构造方法 8 public ...

  8. 【Java】数组不能通过toString方法转为字符串

    java里,所有的类,不管是java库里面的类,或者是你自己创建的类,全部是从object这个类继承的.object里有一个方法就是toString(),那么所有的类创建的时候,都有一个toStrin ...

  9. SpringMVC接收对象数组参数进行封装

    前台代码:注意.contentType : "application/json; charset=utf-8",必须要设置,只有这样SpringMVC才认识这个json数组参数 f ...

  10. java去除数组重复元素的方法

    转载自:https://blog.csdn.net/Solar24/article/details/78672500 import java.util.ArrayList; import java.u ...

随机推荐

  1. Linux下rz命令上传文件失败处理

    在Linux服务器上使用rz命令上传文件时,有时候会遇到文件上传失败,此时会在当前目录下生成一堆大小为0的乱码文件,如下图所示: 这些文件有时候rm命令也无法删除,下面提供两种通过find命令删除的方 ...

  2. 从零开始配置vim(20)——模糊查询

    在讲解vim的基础功能的时候,介绍过了vim的各种查询技巧,在同一个文件中进行搜索的话,那些技巧很有用.在多个文件中我们介绍了使用vim自带的 :grep命令进行搜索,使用quickfix 列表进行跳 ...

  3. Linux命令-tail命令

    Linux命令-tail 命令分析 命令格式 参数 例子 Linux命令-tail 命令分析 tail命令可用于查看文件的内容,通常用来查看日志,加上-f参数就可以查看最新的日志并且不断刷新. 命令格 ...

  4. net8来了

    11 月 15 日开始的为期三天的 .NET Conf 在线活动的开幕日上,.NET 8作为微软的开源跨平台开发平台正式发布..NET 团队着重强调云.性能.全栈 Blazor.AI 和 .NET M ...

  5. Markdown-CSDN自带帮助语法

    这里写自定义目录标题 欢迎使用Markdown编辑器 新的改变 功能快捷键 合理的创建标题,有助于目录的生成 如何改变文本的样式 插入链接与图片 如何插入一段漂亮的代码片 生成一个适合你的列表 创建一 ...

  6. 深度学习基础入门篇[六(1)]:模型调优:注意力机制[多头注意力、自注意力],正则化【L1、L2,Dropout,Drop Connect】等

    深度学习基础入门篇[六(1)]:模型调优:注意力机制[多头注意力.自注意力],正则化[L1.L2,Dropout,Drop Connect]等 1.注意力机制 在深度学习领域,模型往往需要接收和处理大 ...

  7. Python实现栈、队列、双端队列

    栈的实现 class Stack(): def __init__(self): self.items = [] def push(self, item): self.items.append(item ...

  8. CentOS7上systemctl的使用

    CentOS 7.x开始,CentOS开始使用systemd服务来代替daemon,原来管理系统启动和管理系统服务的相关命令全部由systemctl命令来代替. 1.原来的 service 命令与 s ...

  9. MySQL最左匹配原则

    最左匹配原则都是针对联合索引来说的,那么为什么要使用联合索引呢? 一.为什么要使用联合索引? 1.减少开销. 建一个联合索引(col1,col2,col3),实际相当于建了(col1),(col1,c ...

  10. RAPTOR:递归摘要与树形检索的结合,提升RAG检索性能

    RAPTOR:递归摘要与树形检索的结合,提升RAG检索性能 来源:ICLR'24 https://arxiv.org/pdf/2401.18059.pdf 随着 LLM 技术的发展,RAG 的价值也来 ...