This is the Adult superclass which worker and manager extend
This is my driver class which creates the arrays and sorts them
我想做的是按照worker对象的出生年限对employee数组进行排序,并按照manager对象的名称进行排序。我真的不知道从哪里开始,我将不胜感激一些关于如何实际排列对象的帮助。谢谢!
首先,在您的设计中,将Worker实例添加到Engineers数组。您可以做的一件事是在Java中使用Map数据结构。
SortedMap<Integer,String> sm = new TreeMap<Integer,String>();
sm.put(bYear,name);
这将在地图中存储[key,value]对,在这种情况下,索引将是“出生年份”,而值将是“名称”。要遍历地图,您必须使用迭代器:
Iterator i = sm.iterator();
while(i.hasNext()){
Map.Entry m = (Map.Entry)i.next();
int key = (Integer)m.getKey();
String value = (String)m.getValue();
System.out.println("Birth Year : " + key + " Name : " + value);
}
如果要在一个数组中存储Worker对象,并且仍然希望对它们进行排序,则必须在添加之前进行检查,这将需要很多工作。
我想提一件事,您不应该初始化类参数
protected int birthYear;
protected String name;
应该是使用它的方法。希望这会有所帮助,祝您编程愉快。