希望對你有所幫助
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;
}
}