欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

编程诡事2020年6月7日

程序员文章站 2022-04-10 17:01:17
...

今天看到一个练习题,感觉难度适中,就试着做了一下,但是发现不太会做,先来看看题目:编程诡事2020年6月7日
考察知识点非常简单,是对于comparable接口的实现,然后昨天也看了不少博客,实现方法大致如下:

  1. 一开始肯定是需要导入包:

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;//因为我用List接受的ArrayList对象,所以导入了List包
    
  2. 实现compareTo()方法,定义优先顺序,返回值是一个int类型,猜测是通过正负号表示当前对象是否大于参数对象。

  3. 在主方法中调用Collections.sort(),参数是要排序的ArrayList对象(也许支持其他类型,但我没有去尝试研究)。

  4. 调用排序方法时候不知为何Java抛出了数组下标越界异常,我单纯进行抓捕之后便没事了并且顺利运行出结果,但是结果并不正确。

因为逻辑非常简单所以就没有打注释,具体代码如下:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Employee extends Person {
	private int salary;
	private String crop;
	public Employee(String id, String name, String sex, int salary, String crop) {
		super(id, name, sex);
		this.salary = salary;
		this.crop = crop;
	}
	@Override
	public String toString() {
		return "Employee [id=" + this.getId() + ", name=" + this.getName() + ", sex=" + this.getSex() + ", salary=" + this.getSalary() + ", crop=" + getCrop() + "]";
	}
	public int getSalary() {
		return salary;
	}
	public void setSalary(int salary) {
		this.salary = salary;
	}
	public String getCrop() {
		return crop;
	}
	public void setCrop(String crop) {
		this.crop = crop;
	}
	public static void main(String[] args){
		List  personList = new ArrayList();
		personList.add(new Person("00000001","张三","男"));
		personList.add(new Employee("0000000X","广场舞大妈","女",0,"东大街路口第五号"));
		personList.add(new Person("000000000","李四","男"));
		personList.add(new Person("00000002","王五","男"));
		personList.add(new Employee("0000000A","跑堂的李狗蛋","男",100,"同福客栈"));
		personList.add(new Person("000000009","赵六","男"));
		personList.add(new Person("00000013","张大麻子","男"));
		personList.add(new Employee("00000008","迪丽热巴","女",10000,"未知"));
		personList.add(new Employee("00000006","马尔扎哈","女",10000,"未知"));
		personList.add(new Employee("0000000Y","沙扬娜拉","女",10000,"未知"));
		personList.add(new Person("000000017","采花贼","男"));
		for (Object p : personList)
		{
			System.out.println(p.toString());
		}
		try {
			Collections.sort(personList);
		}catch (Exception E){
			
		}
        System.out.println("After sort");
        for (Object p : personList)
		{
			System.out.println(p.toString());
		}
	}
}

class Person implements Comparable<Person>{
	private String id;
	private String name;
	private String sex;
	public Person(String id, String name, String sex) {
		super();
		this.id = id;
		this.name = name;
		this.sex = sex;
	}
	@Override
	public String toString() {
		return "Person [id=" + id + ", name=" + name + ", sex=" + sex + "]";
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	@Override
	public int compareTo(Person arg0) {
		// TODO Auto-generated method stub
		char[] charArrayOne = this.id.toCharArray();
		char[] charArrayTwo = arg0.id.toCharArray();
		if (0 <= charArrayOne.length){
			for (int i = 0;i < charArrayOne.length;i++)
			{
				//System.out.println(i);
				if (charArrayOne[i] == charArrayTwo[i]){
					continue;
				}
				else{
					return charArrayOne[i] - charArrayTwo[i];
				}
			}
		}
		return 0;
	}
}

运行结果如下:编程诡事2020年6月7日
这些是我在编程学习的路上遇到的一些无法理解的知识的所获和所惑,并不严谨,如果您愿意指正我的错误,不胜感激。我的QQ号是:2858885213。

相关标签: 笔记