快速简单的电话簿程序 [英] quicksort for simple phonebook program

查看:80
本文介绍了快速简单的电话簿程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在做一个相当基础的Java课程,我必须设计一个非常简单的电话簿程序,它将需要少量的电话录入(数字可以是预定义的,所以我可以使用数组而不是列表),对它们进行排序并允许它们被搜索。


我只是专注于问题的排序部分那一刻,我不知道从哪里开始!我有两种方法可以接近它:


1)创建一个对象数组,每个对象包含一个名称和一个电话号码的变量,然后使用快速输入用于对对象排序的数组。这个问题(对我而言,无论如何,我确定这对你们来说真的很容易!)是因为我不确定(a)如何将各个对象放到数组中并且( b)如何在这种情况下使用quicksort,以便Name变量用于排序目的。


2)创建一个更简单的字符串数组,其中包含名称为Strings,并有一个第二个数组中相应的电话号码在相应的索引位置。这种方法的问题在于,在String数组上执行快速排序以移动名称时,我不知道如何确保第二个阵列中相应的电话号码也会移动。


非常感谢任何帮助或建议!


Shane

Hi,

I''m doing a fairly basic Java course, and I have to design a very simple phone book programme which will take a small number of phone entries (the number can be predefined so I can use arrays instead of lists), sort them and allow them to be searched.

I''m just concentrating on the sorting part of the problem at the moment, and I''m not sure where to start! There''s two ways I can approach it:

1) Create an array of objects, each object containing a variable for a Name and a Phone Number, and then use quicksort on the array to sort the objects. The problems with this (for me anyway, I''m sure this is really easy for you guys!) is that I''m not sure on (a) how to get the individual objects into the array in the first place and (b) how to use quicksort in this case so that the Name variable is used for sorting purposes.

2) Create a much simpler string array containing the names as Strings, and have a second array with the relevant Phone numbers in the corresponding index position. The problems with this approach is that while performing quicksort on the String array to move the names around, I don''t know how to ensure that the corresponding telephone number in the second array also moves.

Any help or suggestions much appreciated!

Shane

推荐答案

Java中的面向对象方式是实现您的PhoneEntry课程中可比较界面

; String类也实现了这个接口,'

为什么对字符串进行排序很容易。


如果你绝对不想这样做,看看比较器界面,

它可以比较一个类的两个实例,并决定哪一个更大等。

阅读这两个界面的API文档。


永远不要考虑你的替代方案2)将b $ b b物品分开并粘贴它们被视为犯罪行为单独数组中的残余。 Java不是Fortran

,这种做法将不受欢迎。


Java中还有一篇通用堆排序文章s Howtos部分可能是值得阅读的



亲切的问候,


Jos
The Object Oriented way in Java is to implement the Comparable interface
in your PhoneEntry class; the String class also implements this interface, that''s
why it is so easy to sort Strings.

If you absolutely don''t want to do that, have a look at the Comparator interface,
it can compare two instantiations of a class and decide which one is larger etc.
Read the API documentation for those two interfaces.

Never, ever consider your alternative 2) it is considered a criminal offence to rip
objects apart and stick their remnants in separate arrays. Java is not Fortran
and such practice will be frowned upon.

There''s also a ''generic heap sort'' article in Java''s Howtos section that may be
worth reading.

kind regards,

Jos






我正在做一个相当基础的Java课程,我必须设计一个非常好的简单的电话簿程序,它将占用少量电话条目(数字可以预定义,所以我可以使用数组而不是列表),对它们进行排序并允许它们被搜索。


我现在只专注于问题的排序部分,我不知道从哪里开始!我有两种方法可以接近它:


1)创建一个对象数组,每个对象包含一个名称和一个电话号码的变量,然后使用快速输入用于对对象排序的数组。这个问题(对我而言,无论如何,我确定这对你们来说真的很容易!)是因为我不确定(a)如何将各个对象放到数组中并且( b)如何在这种情况下使用quicksort,以便Name变量用于排序目的。


2)创建一个更简单的字符串数组,其中包含名称为Strings,并有一个第二个数组中相应的电话号码在相应的索引位置。这种方法的问题在于,在String数组上执行快速排序以移动名称时,我不知道如何确保第二个阵列中相应的电话号码也会移动。


非常感谢任何帮助或建议!


Shane
Hi,

I''m doing a fairly basic Java course, and I have to design a very simple phone book programme which will take a small number of phone entries (the number can be predefined so I can use arrays instead of lists), sort them and allow them to be searched.

I''m just concentrating on the sorting part of the problem at the moment, and I''m not sure where to start! There''s two ways I can approach it:

1) Create an array of objects, each object containing a variable for a Name and a Phone Number, and then use quicksort on the array to sort the objects. The problems with this (for me anyway, I''m sure this is really easy for you guys!) is that I''m not sure on (a) how to get the individual objects into the array in the first place and (b) how to use quicksort in this case so that the Name variable is used for sorting purposes.

2) Create a much simpler string array containing the names as Strings, and have a second array with the relevant Phone numbers in the corresponding index position. The problems with this approach is that while performing quicksort on the String array to move the names around, I don''t know how to ensure that the corresponding telephone number in the second array also moves.

Any help or suggestions much appreciated!

Shane



如果您喜欢它进行实验。 />

您可以使用#2实现...以避免以后出现更多混淆....


当您添加名称时,您还必须添加同一元素中的数字...


x = 0;

myname = StringName [x];

mynumber = StringNumber [x];


当你搜索它们时,只需使用元素...... 1个元素代表2个值...来自数组....


由于你使用的是quicksort,你应该使用2个临时变量来存储名称和数字......用于交换值....


Jo的回复很好实施。


sukatoa ......

If you like it to have an experiment.

You could use #2 implementation... to avoid more confusions later....

As you add a name, you must also add the number in the same element...

x = 0;
myname = StringName[ x ];
mynumber = StringNumber[ x ];

When you search them, just use the element... 1 element represents 2 values... from arrays....

Since you are using quicksort, you should use 2 temp variables for storing name and number... for swapping values....

Jo''s reply is good to implement.

sukatoa...


#2可以用Map完成。 TreeMap 甚至会将字符串存储在(有效)排序顺序中...不可否认,这比你想要处理的更复杂......
#2 could be accomplished with a Map. A TreeMap would even store the Strings in (effectively) sorted order...Admittedly, this is more complicated than you probably want to deal with...


这篇关于快速简单的电话簿程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆