标签:rem ++ OLE main方法 java 链表 lis 个学生 rgs
//题目:把学生链表按照学号从大到小排序
package Basic50;
import java.util.ArrayList;
public class ArraySort
{
public static class Student//创建学生类(内部类)
{
public int xuehao;
public String name;
public boolean sex;
public Student(int xuehao, String name, boolean sex)
{
super();
this.xuehao = xuehao;
this.name = name;
this.sex = sex;
}
@Override
public String toString()
{
return "Student [xuehao=" + xuehao + ", name=" + name + ", sex=" + sex + "]";
}
public Student()
{
super();
// TODO Auto-generated constructor stub
}
}
public static void main(String[] args)//main方法
{
Student s1 = new Student(22, "li", false);//输入学生信息
Student s2 = new Student(5, "cheng", false);
Student s3 = new Student(4, "sun", true);
Student s4 = new Student(39, "李三", true);
Student s5 = new Student(10, "王五", true);
Student s6 = new Student(12, "张柳", true);
ArrayList a1 = new ArrayList();// 创建原始学生链表
ArrayList a2 = new ArrayList();// 创建排序后的学生链表
a1.add(s1);// 原始学生链表取得数据
a1.add(s2);
a1.add(s3);
a1.add(s4);
a1.add(s5);
a1.add(s6);
Student temp1 = new Student();// 创建两个用于临时存放的学生类型
Student temp2 = new Student();
for (int i = 0; i < a1.size(); i++)
{
for (int k = 0; k < a1.size(); k++)
{
temp1 = (Student) a1.get(i);// 取得原始学生链表的第i个学生数据
temp2 = (Student) a1.get(k);// 取得原始学生链表的第k个学生数据
if (temp2.xuehao > temp1.xuehao)
{
break;// 如果第i个学生学号不是最大,i++
}
if (k == (a1.size() - 1))// 如果k已经是原始链表最后一个,说明第i个是学号最大的
{
a2.add(temp1);
a1.remove(i);
i = -1;// 去掉第i个学生后重新比较
}
}
}
for (int i = 0; i < a2.size(); i++)// 按顺序打印
{
System.out.println(a2.get(i));
}
System.out.println("exit");
}
}
标签:rem ++ OLE main方法 java 链表 lis 个学生 rgs
原文地址:https://www.cnblogs.com/java3280/p/10195108.html