中易网

Java求集合编写

答案:1  悬赏:0  
解决时间 2021-01-17 20:08
  • 提问者网友:沉默菋噵
  • 2021-01-17 14:28
Java求集合编写
最佳答案
  • 二级知识专家网友:从此江山别
  • 2021-01-17 15:34
package com.dy.test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
public class Test {
 public static void main(String[] args) throws IOException {
  
  Student[] stu = new Student[10];
  stu[0] = new Student("20170101", "小明", new Random().nextInt(41) + 60);
  stu[1] = new Student("20170102", "小红", new Random().nextInt(41) + 60);
  stu[2] = new Student("20170103", "小华", new Random().nextInt(41) + 60);
  stu[3] = new Student("20170104", "小风", new Random().nextInt(41) + 60);
  stu[4] = new Student("20170105", "小黄", new Random().nextInt(41) + 60);
  stu[5] = new Student("20170106", "小绿", new Random().nextInt(41) + 60);
  stu[6] = new Student("20170107", "小帅", new Random().nextInt(41) + 60);
  stu[7] = new Student("20170108", "小黑", new Random().nextInt(41) + 60);
  stu[8] = new Student("20170109", "小军", new Random().nextInt(41) + 60);
  stu[9] = new Student("20170110", "小胖", new Random().nextInt(41) + 60);
  
  System.out.println("排序前:");
  for (int i = 0; i < stu.length; i++) {
   System.out.println(stu[i]);
  }
  
  // List集合
  List stuList = new ArrayList();
  for (int i = 0; i < stu.length; i++) {
   stuList.add(stu[i]);
  }
  
  System.out.println("List排序:");
  Collections.sort(stuList); // 排序方法
  for (int i = 0; i < stuList.size(); i++) {
   System.out.println(stuList.get(i));
  }
  
  // Set集合
  Set stuSet = new TreeSet(); // TreeSet自动排序
  for (int i = 0; i < stu.length; i++) {
   stuSet.add(stu[i]);
  }
  System.out.println("Set排序:");
  for (int i = 0; i < stuSet.size(); i++) {
   System.out.println(stuSet.toArray()[i]);
  }
  
  // Map
  Map stuMap = new TreeMap(); // TreeMap自动排序
  for (int i = 0; i < stu.length; i++) {
   stuMap.put(stu[i], i);
  }
  
  System.out.println("Map排序:");
  Object[] keys = stuMap.keySet().toArray();
  for (int i = 0; i < keys.length; i++) {
   System.out.println(keys[i]);
  }
  
 }
}
class Student implements Comparable {
 private String sid;
 private String sname;
 private int score;
 
 public Student(String sid, String sname, int score) {
  super();
  this.sid = sid;
  this.sname = sname;
  this.score = score;
 }
 public String getSid() {
  return sid;
 }
 public void setSid(String sid) {
  this.sid = sid;
 }
 public String getSname() {
  return sname;
 }
 public void setSname(String sname) {
  this.sname = sname;
 }
 public int getScore() {
  return score;
 }
 public void setScore(int score) {
  this.score = score;
 }
 @Override
 public int compareTo(Student o) {
//  return this.score - o.getScore(); // 从低到高
  int r = o.getScore() - this.score; // 从高到低
  return r == 0 ? o.getSid().hashCode() - sid.hashCode() : r;
 }
 @Override
 public String toString() {
  return sname + "(" + sid + ") " + score + "分";
 }
 
}

运行效果:

追问老哥,不对呀
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息!
大家都在看
推荐信息