我是靠谱客的博主 光亮河马,这篇文章主要介绍LinkedList的使用练习,现在分享给大家,希望可以做个参考。

题目

设计一个Student类,该类中包括学生的姓名和成绩。创建Student类的5个对象,如下所示:

姓名

成绩

刘德华

90

张学友

80

刘杰

65

章子怡

100

周迅

60

将以上5个对象放入LinkedList中,完成如下操作和统计:

  1. 输出LinkedList中的对象个数。
  2. 删除姓名为“刘杰”的学生信息,并输出LinkedList中现有学生信息。
  3. 将姓名为“刘德华”的学生成绩改为95。
  4. 输出成绩不及格的学生姓名。

student类

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package 实验四; public class Student { private String name; private int crose; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getCrose() { return crose; } public void setCrose(int crose) { this.crose = crose; } public Student(String name, int crose) { super(); this.name = name; this.crose = crose; } }

测试类

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package 实验四; import java.util.*; public class Studenttest { public static void main(String[] args) { // TODO 自动生成的方法存根 Scanner sc = new Scanner(System.in); Student st1 = new Student("刘德华",90); Student st2 = new Student("张学友",80); Student st3 = new Student("刘杰",65); Student st4 = new Student("章子怡",100); Student st5 = new Student("周迅",60); //创建一个链表 LinkedList<Student> lin = new LinkedList<Student>(); //向链表中添加元素 lin.add(st1); lin.add(st2); lin.add(st3); lin.add(st4); lin.add(st5); //输出链表中元素的个数 System.out.println(lin.size()); //创建lin链表的迭代器 Iterator<Student> it = lin.iterator(); System.out.println(); // //hasnext没有指针下移操作,只判断是否存在下一元素 // while(it.hasNext()) { // //next指针下移,返回指针指向的元素 // System.out.print(it.next().getName()); // System.out.println(it.next().getCrose()); // } while(it.hasNext()) { //equals判断是否存在 if("刘杰".equals(it.next().getName())) { //remove删除元素 it.remove(); } } //遍历链表元素 for(Student s : lin) { System.out.println(s.getName()+" "+s.getCrose()); } for(Student s : lin) { if("刘德华".equals(s.getName())) { //更改元素数据 s.setCrose(95); } } System.out.println(); for(Student s : lin) { System.out.println(s.getName()+" "+s.getCrose()); } System.out.println(); for(Student s : lin) { if(s.getCrose()<60) { System.out.println(s.getName()); } } } }

最后

以上就是光亮河马最近收集整理的关于LinkedList的使用练习的全部内容,更多相关LinkedList内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(99)

评论列表共有 0 条评论

立即
投稿
返回
顶部