1、编写学生类,该类定义了 3 个属性:学号、姓名、成绩。可以通过构造方法设置 3 个属性的内容,并覆写 Object 类中的 toString()方法,在 List 集合中加入 5 个学生对象,并将内容输出,之后使用比较器将对象的内容进行排序并显示在屏幕上。
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Student> list = new ArrayList<>();
list.add(new Student("002", "张三", 80));
list.add(new Student("004", "李四", 90));
list.add(new Student("003", "王五", 70));
list.add(new Student("001", "赵六", 90));
list.add(new Student("005", "钱七", 90));
System.out.println("排序前:");
for (Student student : list) {
System.out.println(student);
}
Collections.sort(list);
System.out.println("排序后:");
for (Student student : list) {
System.out.println(student);
}
}
}
class Student implements Comparable<Student> {
private String id;
private String name;
private int score;
public Student(String id, String name, int score) {
this.id = id;
this.name = name;
this.score = score;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
@Override
public String toString() {
return "Student{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", score=" + score +
'}';
}
@Override
public int compareTo(Student stu) {
if (this.id.compareTo(stu.id) > 0) {
return 1;
}else if(this.id.compareTo(stu.id) < 0){
return -1;
}else {
if(this.score > stu.score){
return -1;
}else if (this.score < stu.score){
return 1;
}else {
return 0;
}
}
}
}
2、完成一个学生管理程序,使用学号作为键添加 5 个学生对象,并可以将全部的信息保存在文件中,可以实现对学生信息的学号查找、输出全部学生信息的功能。
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception{
File file = new File("student.txt");
OutputStream out = new FileOutputStream(file);
Map<String,Student> map = new HashMap<String,Student>();
map.put("1",new Student(2020001,"张三",91));
map.put("2",new Student(2022008,"李四",92));
map.put("3",new Student(2019003,"王五",99));
map.put("4",new Student(2022002,"赵六",90));
map.put("5",new Student(2021002,"田七",95));
String str = map.toString();
byte[] bytes = str.getBytes();
out.write(bytes);
out.close();
Student val = map.get("1");
System.out.println("1号学生的信息是:"+val);
System.out.println("所有学生的信息是:");
Collection<Student> values = map.values();
Iterator<Student> iter = values.iterator();
while (iter.hasNext()) {
Student stu = iter.next();
System.out.println(stu);
}
}
}
class Student{
private int num;
private String name;
private float score;
public Student(int num,String name,float score){
this.num=num;
this.name=name;
this.score=score;
}
@Override
public String toString() {
return "Student{" +
"num=" + num +
", name='" + name + '\'' +
", score=" + score +
'}';
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getScore() {
return score;
}
public void setScore(float score) {
this.score = score;
}
}
3、编写一个双向链表。
代码实现见:https://blog.csdn.net/qq_58284486/article/details/123531837
//代码实现见:https://blog.csdn.net/qq_58284486/article/details/123531837
class ListNode {
public int val;
public ListNode next;//前驱信息
public ListNode prev;//后继信息
public ListNode(int val) {
this.val = val;
}
}
public class DoubleLinkedList {
public ListNode head;//标记双向链表的头结点
public ListNode last;//标记双向链表的尾结点
//头插法
public void addFirst(int data) {
ListNode node = new ListNode(data);
if(this.head == null) {
this.head = node;
this.last = node;
} else {
node.next = this.head;
this.head.prev = node;
this.head = node;
}
}
//尾插法
public void addLast (int data) {
ListNode node = new ListNode(data);
if(this.head == null) {
this.head = node;
this.last = node;
} else {
this.last.next = node;
node.prev = this.last;
this.last = node;
}
}
//寻找要插入的位置
public ListNode findIndex(int index) {
int pos = 0;
ListNode cur = this.head;
while(pos != index) {
cur = cur.next;
pos++;
}
return cur;
}
//指定位置插入key
public void addIndex(int index, int data) {
if(index < 0 || index > size()) {
throw new RuntimeException("操作位置不合法");
}
if(index == 0) {
addFirst(data);
return;
}
if(index == size()) {
addLast(data);
return;
}
ListNode node = new ListNode(data);
ListNode cur = findIndex(index);
cur.prev.next = node;
node.prev = cur.prev;
node.next = cur;
cur.prev = node;
}
//判断双向链表是否包含关键字key
public boolean contains(int key) {
ListNode cur = this.head;
while(cur != null) {
cur = cur.next;
if(cur.val == key) {
return true;
}
}
return false;
}
//打印双向链表,正向打印
public void display() {
ListNode cur = this.head;
while (cur != null) {
System.out.print(cur.val+" ");
cur = cur.next;
}
}
//打印双向链表,反向打印
public void display1() {
ListNode cur = this.last;
while(cur != null) {
System.out.print(cur.val+" ");
cur = cur.prev;
}
}
//按值查找结点位置
public ListNode findKeyIndex(int key) {
ListNode cur = this.head;
while(cur != null) {
if(cur.val == key) {
return cur;
}
cur = cur.next;
}
return null;
}
//删除第一次出现关键字为key的节点
public void remove(int key) {
if(this.head == null) {
throw new RuntimeException("链表为空,无法进行删除操作");
}
if(this.head.val == key) {
this.head = this.head.next;
this.head.prev = null;
return;
}
ListNode cur = findKeyIndex(key);
if(cur == last) {
this.last = this.last.prev;
this.last.next = null;
return;
}
cur.prev.next = cur.next;
cur.next.prev = cur.prev;
}
//删除所有值为key的节点
public void removeAll(int key) {
if(this.head == null) {
throw new RuntimeException("链表为空,无法进行删除操作");
}
ListNode cur = this.head.next;
while(cur != this.last) {
if(cur.val == key) {
cur.prev.next = cur.next;
cur.next.prev = cur.prev;
}
cur = cur.next;
}
if(this.head.val == key) {
this.head = this.head.next;
this.head.prev = null;
}
if(this.last.val == key) {
this.last = this.last.prev;
this.last.next = null;
}
}
//得到单链表的长度
public int size() {
ListNode cur = this.last;
int size = 0;
while(cur != null) {
cur = cur.prev;
size++;
}
return size;
}
//清空单链表
public void clear(){
this.head = null;
this.last = null;
}
}
测试:
public class Main {
public static void main(String[] args) {
System.out.println("头插法");
DoubleLinkedList dllFirst = new DoubleLinkedList(); //创建双向链表对象,头插法
for(int i = 0; i < 10; i++) {
dllFirst.addFirst(i);
}
System.out.println("链表长度为:" + dllFirst.size());
dllFirst.display();
System.out.println();
System.out.println("在1号位置插入10086:");
dllFirst.addIndex(1, 10086);
dllFirst.display();
System.out.println();
System.out.println("是否包含10086?");
System.out.println(dllFirst.contains(10086));
System.out.println("10086的节点位置在:"+dllFirst.findKeyIndex(10086));
System.out.println("删除10086");
dllFirst.removeAll(10086);
dllFirst.display();
}
}
4、编程通过栈的方式将任意输入的字符串内容进行逆序输出。
import java.util.Stack;
public class Main {
public static void main(String[] args) {
Stack<String> stack = new Stack<String>();
stack.push("人!");
stack.push("先");
stack.push("你");
stack.push("日");
stack.push("我");
stack.push("山本");
while (!stack.isEmpty()) {
System.out.print(stack.pop());
}
}
}