import java.util.*; public class TreeSetExample { public static void main(String args[ ]) { TreeSet<Student> mytree=new TreeSet<Student>(); for(int i=0;i<5;i++) { Scanner read=new Scanner(System.in); System.out.println("学生的姓名:"); String name=read.nextLine( ); System.out.println("输入分数(整数):"); int score=read.nextInt( ); 【代码1】 // 向mytree添加Student对象 } Iterator<Student> te=【代码2】 // mytree返回Iterator对象 while(【代码3】) // 判断te中是否存在元素 { Student stu=【代码4】 // 返回te中的下一个元素 System.out.println(""+stu.name+" "+stu.english); } } } class Student implements Comparable { int english=0; String name; Student(int e,String n) { english=e; name=n; } public int compareTo(Object b) { Student st=(Student)b; return (this.english-st.english); } }
时间: 2023-11-22 19:53:19 浏览: 75
代码1:
```java
mytree.add(new Student(score, name));
```
代码2:
```java
Iterator<Student> te = mytree.iterator();
```
代码3:
```java
while(te.hasNext())
```
代码4:
```java
Student stu = te.next();
```
相关问题
修改代码使其按照姓名首字母数字排序import java.util.Comparator; import java.util.Iterator; import java.util.TreeSet; public class sort_student { public static void main(String[] args) { //创建 TreeSet 对象时指定排序器 Comparator TreeSet<student> s = new TreeSet<>(new Comparator<student>() { public int compare(student o1, student o2) { return o1.getAge() - o2.getAge(); } }); // 按照年龄插入数据 s.add(new student("Tom", 18)); s.add(new student("Bob", 14)); s.add(new student("Jack", 16)); s.add(new student("Alice", 13)); s.add(new student("Koltin", 22)); s.add(new student("Abuton", 19)); // 迭代 TreeSet 集合,按照年龄从小到大输出学生信息 Iterator<student> it = s.iterator(); while (it.hasNext()) { student tt = it.next(); sop(tt.getName() + "," + tt.getAge()); } } public static void sop(Object obj) { System.out.println(obj); } }
import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;
public class sort_student {
public static void main(String[] args) {
// 创建 TreeSet 对象时指定排序器 Comparator
TreeSet<student> s = new TreeSet<>(new Comparator<student>() {
public int compare(student o1, student o2) {
int result = o1.getName().compareTo(o2.getName());
if (result == 0) {
result = o1.getAge() - o2.getAge();
}
return result;
}
});
// 按照姓名和年龄插入数据
s.add(new student("Tom", 18));
s.add(new student("Bob", 14));
s.add(new student("Jack", 16));
s.add(new student("Alice", 13));
s.add(new student("Koltin", 22));
s.add(new student("Abuton", 19));
// 迭代 TreeSet 集合,按照姓名和年龄从小到大输出学生信息
Iterator<student> it = s.iterator();
while (it.hasNext()) {
student tt = it.next();
sop(tt.getName() + "," + tt.getAge());
}
}
public static void sop(Object obj) {
System.out.println(obj);
}
}
重写package shiyan4_3; import java.awt.; import java.awt.event.; import java.util.; import javax.swing.; public class StudentFrame extends JFrame implements ActionListener { JTextArea showArea; JTextField inputName,inputScore; JButton button; TreeSet<Student> treeSet; StudentFrame() { treeSet = new TreeSet<Student>(); showArea=new JTextArea(); showArea.setFont(new Font("",Font.BOLD,20)); inputName=new JTextField(5); inputScore=new JTextField(5); button=new JButton("确定"); button.addActionListener(this); JPanel pNorth=new JPanel(); pNorth.add(new JLabel("Name:")); pNorth.add(inputName); pNorth.add(new JLabel("Score:")); pNorth.add(inputScore); pNorth.add(button); add(pNorth,BorderLayout.NORTH); add(showArea,BorderLayout.CENTER); setSize(300,320); setVisible(true); validate(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent e) { String name=inputName.getText(); int score=0; try{ score=Integer.parseInt(inputScore.getText().trim()); if(name.length()>0) { Student stu=new Student(name,score); treeSet.add(stu); // treeSet添加stu show(treeSet); } } catch(NumberFormatException exp) { inputScore.setText("请输入数字字符"); } } public void show(TreeSet tree) { showArea.setText(null); Iterator<Student> te = tree.iterator(); //获得Iterator对象te while(te.hasNext()) { Student stu=te.next(); // te返回其中的下一个元素 showArea.append("Name:"+stu.getName()+" Score: "+stu.getScore()+"\n"); } } public static void main(String args[]) { new StudentFrame(); } }
package shiyan4_3;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class StudentFrame extends JFrame implements ActionListener {
private JTextArea showArea;
private JTextField inputName, inputScore;
private JButton button;
private TreeSet<Student> treeSet;
public StudentFrame() {
treeSet = new TreeSet<Student>();
showArea = new JTextArea();
showArea.setFont(new Font("", Font.BOLD, 20));
inputName = new JTextField(5);
inputScore = new JTextField(5);
button = new JButton("确定");
button.addActionListener(this);
JPanel pNorth = new JPanel();
pNorth.add(new JLabel("Name:"));
pNorth.add(inputName);
pNorth.add(new JLabel("Score:"));
pNorth.add(inputScore);
pNorth.add(button);
add(pNorth, BorderLayout.NORTH);
add(showArea, BorderLayout.CENTER);
setSize(300, 320);
setVisible(true);
validate();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
String name = inputName.getText();
int score = 0;
try {
score = Integer.parseInt(inputScore.getText().trim());
if (name.length() > 0) {
Student stu = new Student(name, score);
treeSet.add(stu);
show(treeSet);
}
} catch (NumberFormatException exp) {
inputScore.setText("请输入数字字符");
}
}
public void show(TreeSet<Student> tree) {
showArea.setText(null);
Iterator<Student> te = tree.iterator();
while (te.hasNext()) {
Student stu = te.next();
showArea.append("Name:" + stu.getName() + " Score: " + stu.getScore() + "\n");
}
}
public static void main(String args[]) {
new StudentFrame();
}
}
阅读全文