Java 按字母顺序向数组中添加元素
我正在编写一个程序,将电话条目添加到员工电话目录中,我希望按字母顺序(使用姓氏)将元素添加到数组中,而不是先添加元素,然后调用数组。每次添加新条目时都进行排序,因为这样会降低效率。这是我到目前为止的一些代码,我不知道如何将数组中的每个元素与下面的元素进行比较,以此类推Java 按字母顺序向数组中添加元素,java,arrays,Java,Arrays,我正在编写一个程序,将电话条目添加到员工电话目录中,我希望按字母顺序(使用姓氏)将元素添加到数组中,而不是先添加元素,然后调用数组。每次添加新条目时都进行排序,因为这样会降低效率。这是我到目前为止的一些代码,我不知道如何将数组中的每个元素与下面的元素进行比较,以此类推 public class ArrayDirectory implements Directory { Entry [] directory = new Entry [50]; @Override public void add
public class ArrayDirectory implements Directory {
Entry [] directory = new Entry [50];
@Override
public void addEntry(String initials, String surname, int extension) {
//Entries are added here in alphabetical order
}
这是我的入门课-
public class Entry {
private String initals,surname;
private int extention;
public Entry(String initals, String surname, int extention){
this.initals = initals;
this.surname = surname;
this.extention = extention;
}
public String getInitals(){
return initals;
}
public String getSurname(){
return surname;
}
public int getExtention(){
return extention;
}
}
有什么建议吗,我可以忽略比较吗?谢谢
编辑-应该注意到我被要求使用数组。很抱歉给你带来了困惑
编辑2:更新了我的addEntry方法并覆盖了条目中的compareTo-
public void addEntry(String initials, String surname, int extension) {
for (int i = 0; i < directory.length; i++) {
if (directory[i] != null) {
int y = directory[i].getSurname().compareTo(surname);
if (y == 1) {
int position = i;
break;
}
} else if (directory[i] == null) {
int position = i;
break;
}
}
}
我不确定在找到正确的位置后如何将数组中的元素向右移动?感谢您的帮助。您可以将
条目设置为可比较,并在其中实现比较。但在这种情况下,您实际上不必这样做,因为String
已经具有可比性
因为这是一个家庭作业,我认为最好只是给你一些关于如何继续的建议,而不是交给你代码-
在方法中,不需要对数组进行排序,只需将其插入数组中的正确位置即可
从第一个索引开始循环遍历数组
当您通过数组中的每个元素时,必须检查以下两个条件
元素是否为空
当前元素的姓氏
是否大于方法的姓氏
-参数
一旦找到满足上述任何条件的元素,记录索引并中断循环
然后,从该索引开始,将其余元素向右移动
最后,为提供的参数创建一个新的Entry
实例,并将其设置为该索引
注意:这并不考虑阵列中空间不足的情况
更新:
我想你把我的答案和大卫·华莱士的答案弄混了。我的建议不是实施compareTo
。而且,你至少尝试了一下,然后回来了,这很好
int position = -1; //declare the position outside (if declared inside, it's not visible outside the loop)
for (int i = 0; i < directory.length; i++) {
// position = i; just assign value of i inside the loop
}
//use the position after the loop
int j = position; // start at position
Entry temp = null; // temp will temporarily hold the entry at the next index
while(true) {
temp = directory[j + 1]; // since we need move entry at j to j+1, first we need save the entry at j+1
directory[j + 1] = directory[j]; // entry at j to j+1
if(temp == null) { // if the next entry is null, don't really need to move no more, so break
break;
}
}
// finally place new entry at index position
directory[position] = //the new Entry
int位置=-1//在外部声明位置(如果在内部声明,则在循环外部不可见)
for(int i=0;i
使条目
实现可比
,并在条目
类中编写适当的比较方法。然后,在insert
方法中
- 使用
Arrays.binarySearch
在数组中找到插入条目的正确位置
- 使用
System.arraycopy
将数组中位于适当位置之后的所有内容向右移动一个位置李>
- 设置适当的条目
您需要查看Javadoc中的数组.binarySearch
和系统.arraycopy
如果不必使用数组,则使用错误的数据结构
无论您需要通过何种途径实施:
public class Entry implements Comparable<Entry>{
..
@Override
public int compareTo(Entry other) {
// TODO Auto-generated method stub
return this.surname.compareTo(other.getSurname());
}
..
公共类条目实现了可比较的{
..
@凌驾
公共整数比较(其他条目){
//TODO自动生成的方法存根
返回这个.namite.compareTo(other.getnamame());
}
..
考虑使用SortedSet:
Set<Entry> map = new TreeSet<Entry>();
map.add(new Entry("JEH", "Hamlet", 123));
map.add(new Entry("AAC", "Adams", 123));
map.add(new Entry("FAM", "Monti", 321));
Set map=new TreeSet();
添加地图(新条目(“杰赫”,“哈姆雷特”,123));
添加地图(新条目(“AAC”,“Adams”,123));
地图添加(新条目(“FAM”,“Monti”,321));
这将按所需的顺序打印。如果必须使用数组,则需要在插入时对其进行排序。首先,除非必须使用数组,否则不要使用数组。请改用集合-它们更易于处理,并且支持您通常希望对一组对象执行的许多操作
在您的情况下,a将是一个不错的选择。如果您只想在这种用法中(而不是一般情况下)按姓氏对条目进行排序,您可以将customerComparator
传递给:
Set directory=new TreeSet(new Comparator(){
@凌驾
公共整数比较(条目o1、条目o2){
返回o1.getNashName().compareTo(o2.getNashName());
}
});
如果您总是希望使用姓氏对Entry
对象进行排序,请让您的Entry
类实现Comparable
,并将代码移动到Entry
类的compareTo()方法中。是否必须使用数组?尝试读取此数组。sort()对于您正在做的事情来说不是特别有效…您将花费的时间将用于移动数组的其余部分。为什么不使用树集
?我将如何正确地移动元素?我已经更新了上面的代码。谢谢
Set<Entry> map = new TreeSet<Entry>();
map.add(new Entry("JEH", "Hamlet", 123));
map.add(new Entry("AAC", "Adams", 123));
map.add(new Entry("FAM", "Monti", 321));
Set<Entry> directory = new TreeSet<>(new Comparator<Entry>() {
@Override
public int compare(Entry o1, Entry o2) {
return o1.getSurname().compareTo(o2.getSurname());
}
});