希望对你有所帮助
package com.neo.project;import java.util.ArrayList;import java.util.Collections;public class StudentModel {private String id;private String name;private Integer score;public void setId(String id) {this.id = id;}public String getId() {return id;}public void setName(String name) {this.name = name;}public String getName() {return name;}public void setScore(Integer score) {this.score = score;}public Integer getScore() {return score;}public ArrayList<Integer> sortByScore(ArrayList<Integer> scoreList) {Collections.sort(scoreList);return scoreList;}}复制代码
package com.neo.project;import java.util.ArrayList;public class TestStudent {private String[] studentIds = {"22", "11", "66", "44"}; private String[] studentNames = {"Neo", "Yen", "Roger", "Danny"};private Integer[] studentScores = {98, 58, 75, 41};private ArrayList<StudentModel> studentList = new ArrayList<StudentModel>();public TestStudent() {createStudentModel();outputStudentModel();sortByScore();}private void sortByScore() {ArrayList<Integer> scoreList = new ArrayList<Integer> ();if (isEmptyList(studentList)) {return;}for (int i = 0; i < studentList.size(); i++) {Integer score = studentList.get(i).getScore();scoreList.add(score);}if (isEmptyList(scoreList)) {return;}ArrayList<Integer> sortedList = (new StudentModel()).sortByScore(scoreList);System.out.println("\nSort by score:");for (int i = 0; i < sortedList.size(); i++) {System.out.println(sortedList.get(i));}}private void outputStudentModel() {if (isEmptyList(studentList)) {return;}for (int i = 0; i < studentList.size(); i++) {StudentModel model = studentList.get(i);String id = model.getId();String name = model.getName();Integer score = model.getScore();System.out.println("id = " + id + ", name = " + name + ", score = " + score);}}private void createStudentModel() {StudentModel studentModel = null;for (int i = 0; i < studentIds.length; i++) {studentModel = new StudentModel();String id = studentIds[i];String name = studentNames[i];Integer score = studentScores[i];studentModel.setId(id);studentModel.setName(name);studentModel.setScore(score);studentList.add(studentModel);}}private boolean isEmptyList(ArrayList list) {if (list == null || list.size() == 0) {return true;}return false;}public static void main(String[] args) {new TestStudent();}}复制代码
yt2001728
回答数:94 | 被采纳数:115
如果你需要构造类的话 可以这样写
package com.neo.test;public class StaticStudentModel {private String id;private String name;public StaticStudentModel(String id, String name) {if (isEmpty(id)) {System.out.println("student id should not be empty.");return;}if (isEmpty(name)) {System.out.println("student name should not be empty.");return;}this.id = id;this.name = name;}private boolean isEmpty(String str) {if (str == null || str.trim().length() == 0) {return true;}return false;}}复制代码
package com.neo.test;public class CustomizedStudentModel extends StaticStudentModel {private Integer score;public CustomizedStudentModel(String id, String name, Integer score) {this(id, name);this.score = score;}public CustomizedStudentModel(String id, String name) {super(id, name);}public static void main(String[] args) {}}复制代码
package cn.lusha.y09m03; //D 将该类设置到指定包中
public class Test {
// 声明其属性
private String str;
private int integer;
// 声明其构造函数
public Test() {
}
public void specialMethod(){
// C 按要求写出一些特定方法
}
// 在该类中写出MAIN函数测试以上编写的方法
public static void main(String[] args) {
new Test().specialMethod();
}
/**
*为每一个属性设置GET和SET方法
* @return
*/
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
public int getInteger() {
return integer;
}
public void setInteger(int integer) {
this.integer = integer;
}
}