Java怎样用数组创建对象,并对对象里的属性排序?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java怎样用数组创建对象,并对对象里的属性排序?相关的知识,希望对你有一定的参考价值。

public class Employee 

public Employee() 
super();
// TODO Auto-generated constructor stub



public Employee(String no, String name, Float salary) 
super();
this.no = no;
this.name = name;
this.salary = salary;


private String no;// 工号
private String name;// 姓名
private Float salary = 0f;// 工资

public String getNo() 
return no;


public void setNo(String no) 
this.no = no;


public String getName() 
return name;


public void setName(String name) 
this.name = name;


public Float getSalary() 
return salary;


public void setSalary(Float salary) 
this.salary = salary;



@Override
public String toString() 
return "Employee [no=" + no + ", name=" + name + ", salary=" + salary + "]";





public class TestEmployee 

public static void main(String[] args) 
TestEmployee testEmployee = new TestEmployee();
Employee[] emps = testEmployee.getEmployees();
emps = testEmployee.orderBySalary(emps);
for(int i = 0; i < emps.length; i++) 
System.out.println(emps[i]);



/**
 * 获取一个随机工资数 3-5K
 * @return
 */
public Float getRandomFloat() 
DecimalFormat dcmFmt = new DecimalFormat("0.00");
Random rand = new Random();
float f = 0f;
while(f < 3000) 
f = rand.nextFloat() * 5000;

return Float.parseFloat(dcmFmt.format(f));


/**
 * 获取员工
 * @return 返回Employee[] 数组  length = 50
 */
public Employee[] getEmployees() 
Employee[] emps = new Employee[50];
for(int i = 0; i < 50 ; i++) 
String no = "no" + i;// 初始化员工号
String name = "name" + i;// 初始化姓名
Float salary = getRandomFloat();// 随机产生一个工资数
emps[i] = new Employee(no, name, salary);


return emps;


/**
 * 根据工资高低 进行排序
 * @param emps
 * @return
 */
public Employee[] orderBySalary(Employee[] emps) 
for(int i = 0; i < emps.length; i++) 
for(int j = i + 1; j < emps.length; j++) 
if(emps[i].getSalary() < emps[j].getSalary()) 
Employee temp = emps[i];
emps[i] = emps[j];
emps[j] = temp;



return emps;




参考技术A 用TreeSet储存学生类,用到了TreeSet,学生类需要重写hashCode和equal方法来防止出现重复对象,TreeSet是有序集合,如果要自定义对象大小比较方法,需要在学生类中重写compareTo方法,
public int compareTo(Object obj)
if(!(obj instanceof Student))
throw new RuntimeException("不是学生对象");

Student stu=(Student)obj;
if(this.age>stu.age)
return 1;
if(this.age==stu.age)
return this.name.compareTo(stu.name);
return -1;

然后
public static void main(String[] args)
TreeSet ts= new TreeSet();
ts.add(new Student("lisi01",22));
ts.add(new Student("lisi02",20));
ts.add(new Student("lisi03",18));
ts.add(new Student("lisi04",25));
ts.add(new Student("lisi05",18));

Iterator it = ts.iterator();
while(it.hasNext())
Student stu = (Student) it.next();
System.out.println("姓名:"+stu.getName()+" 年龄:"+stu.getAge());



输出的就是按年龄排序的

java中怎样对一个整数数组进行降序排序

int[] array = 1,2,5,8,12; //随便定义一个数组,用冒泡排序法

for (int i = 0;i < array.length;i++)
for(int j = i;j < array.length;j++)
if (array[i] < array[j])
int temp = array[i];
array[i] = array[j];
array[i] = temp;




或者你有一个数组
List list = new ArrayList();
list.sort();
参考技术A 方法很多 java有自带的方法sort 同时也可以自己写排序算法
冒泡
快排
选择
插入
推排
等等
相应的算法你可以自己去搜索

冒泡排序的代码:
public class Arraysort1
public static void main(String args[])
int score[]=67,89,87,69,90,100,75,90;
long starttime=System.nanoTime();

for(int i=1;i<=100000;i++)
sort(score);

long endtime=System.nanoTime();
System.out.println("Time taken by program:"+(endtime-starttime)+"ns");


public static void sort(int temp[])
for(int i=1;i<temp.length;i++)
for(int j=temp.length-1;j>i;j--)
if(temp[j]<temp[j-1])
int x=temp[j];
temp[j]=temp[j-1];
temp[j-1]=x;




快速排序的代码:
public class Arraysort2
public static void main(String args[])
int score[]=67,89,87,69,90,100,75,90;
long starttime=System.nanoTime();

for(int i=1;i<=100000;i++)
java.util.Arrays.sort(score);


long endtime=System.nanoTime();
System.out.println("Time taken by program:"+(endtime-starttime)+"ns");
参考技术B int[] array = 1,2,5,8,12; //随便定义一个数组,用冒泡排序法

for (int i = 0;i < array.length;i++)
for(int j = i;j < array.length;j++)
if (array[i] < array[j])
int temp = array[i];
array[i] = array[j];
array[i] = temp;


参考技术C 二分排序
例:
int []a=4,2,1,6,3,6,0,-5,1,1;
int i,j;
int low,high,mid;
int temp;
for(i=1;i<10;i++)
temp=a[i];
low=0;
high=i-1;
while(low<=high)
mid=(low+high)/2;
if(a[mid]>temp)
high=mid-1;
else
low=mid+1;

for(j=i-1;j>high;j--)
a[j+1]=a[j];
a[high+1]=temp;

for(i=0;i<10;i++)
System.out.printf("%d",a[i]);

以上是关于Java怎样用数组创建对象,并对对象里的属性排序?的主要内容,如果未能解决你的问题,请参考以下文章

怎么用java程序对集合里的对象按对象的某个属性排序,这个属性是日期(YYYY-MM-DD hh:mm),最好有个例子。

java 怎样传递数组对象

java 对象数组排序的问题,急急急急

java中怎样对一个整数数组进行降序排序

java中怎么 对数组里的 元素 按出现的顺序排序

Java Sorting:按属性对对象数组进行排序,对象不允许使用Comparable