冒泡排序:对成绩从低到高进行排序(JAVA)

数组排序:采用对象数组的方式来存储学生对象

缺点:人数变动时,程序需要较大修改

源代码

public class student  {
private String name;
private int age;
private double grade;
public student(String name,int age,double grade) {
	this.name=name;
	this.age=age;
	this.grade=grade;
}
public String getName() {
	return name;
}
public int getAge() {
	return age;
}
public double getGrade() {
	return grade;
}
}


public class studentclass {

	private student[] stus;
	private int size;
	
	public studentclass() {
		size=0;
		stus=null;
	}
     public void createclass() {
    	 String names[]= {"张三","王五","李四","赵六","孙琦"};
    	 double grades[]= {89,90,78,85,73};
    	 int ages[]= {17,20,17,18,19};
    	 size=names.length;
    	 stus=new student[size];
    	 for(int i=0;istus[j].getGrade()) {
    				 temp=stus[j-1];
    				 stus[j-1]=stus[j];
    				 stus[j]=temp;
    			 }
    		 }
    	 }
     }
     public String output() {
    	 StringBuilder studentInfo=new StringBuilder();
    	 for(int i=0;i
public class test {

	public static void main(String[] args) {
		
  studentclass sclass=new studentclass();
  sclass.createclass();
  System.out.println("原始序列:");
  System.out.println(sclass.output());
  sclass.sort();
  System.out.println("数组冒泡排序结果:");
  System.out.println(sclass.output());
	}
}


运行结果

冒泡排序:对成绩从低到高进行排序(JAVA)_第1张图片



你可能感兴趣的:(Java)