对并行数组进行排序
Java 初学者使用旧教科书和 Head First: Java 书籍来解决一些问题。
我有三个并行的数组。我需要能够根据用户选择按标题、作者或页数进行排序。我可以使用 Arrays.sort() 对一个数组进行排序,但我对如何对其他两个数组进行排序以与新排序的数组相对应感到困惑。
假设我对 BookTitle
数组进行排序,但我需要显示其各自数组的正确作者和页数,我被难住了。
do
{
entry = JOptionPane.showInputDialog(null,
"Enter your sort preference: \n" +
"T = Sort by TITLE\n" +
"A = Sort by AUTHOR\n" +
"P = Sort by PAGE Count");
c = entry.charAt(0);
switch (c)
{
case 't':
case 'T':
Arrays.sort(BookTitle);
for (int x = 0; x < BookTitle.length; x++)
{
msg += ("Title: " + BookTitle[x] + "\n");
}
JOptionPane.showMessageDialog(null, msg);
isValid = true;
break;
case 'a':
case 'A':
isValid = true;
break;
case 'p':
case 'P':
isValid = true;
break;
default:
JOptionPane.showMessageDialog(null, "Invalid entry");
break;
}
} while (isValid == false);
Beginner in Java using an old textbook and Head First: Java books to figure some stuff out.
I have three arrays all parallel. I need to be able to sort by Title, Author, or Page Count based on user selection. I can sort one using Arrays.sort()
but I'm getting hung up on how to sort the other two arrays to correspond to the new sorted one.
Say I sort the BookTitle
array, but I'm going to need to display the proper author and page count of its respective arrays and I'm stumped.
do
{
entry = JOptionPane.showInputDialog(null,
"Enter your sort preference: \n" +
"T = Sort by TITLE\n" +
"A = Sort by AUTHOR\n" +
"P = Sort by PAGE Count");
c = entry.charAt(0);
switch (c)
{
case 't':
case 'T':
Arrays.sort(BookTitle);
for (int x = 0; x < BookTitle.length; x++)
{
msg += ("Title: " + BookTitle[x] + "\n");
}
JOptionPane.showMessageDialog(null, msg);
isValid = true;
break;
case 'a':
case 'A':
isValid = true;
break;
case 'p':
case 'P':
isValid = true;
break;
default:
JOptionPane.showMessageDialog(null, "Invalid entry");
break;
}
} while (isValid == false);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
第一个选项如上所述,将它们放入一个类中并使其具有可比性,例如 为什么 Java 类应该实现可比性? 。
这只允许您比较一个值,我认为有一种方法可以选择要排序的内容,但我不知道。
另一种方法...是实现你自己的排序方法。这可能需要一些工作,但至少做一次可能会很好,这样您就可以学会它。插入和选择排序非常容易掌握,并且允许您并行移动每个数组中的元素。
更新:如上所述,您可以有多个比较器,请查看 这个网站,因为它似乎拥有您需要的一切。
First option is as above, chuck them into one class and make it comparable like Why should a Java class implement comparable?.
This only allows you to compare on one value, I think there is a way to choose what to sort on, but not that I know.
The other way... is to implement your own sort method. It can be a bit of work, but is probably good to do at least once so you can learn it. Insertion and selection sort are pretty easy to master, and the will allows you to move elements in each array in parallel.
UPDATE: As above you can have multiple compartors, check out this website as it seems to have everything you need.
您可以创建一个具有作者、标题、页码这三个属性的类。然后您可以创建 3 个比较器,分别根据作者、标题和页面进行比较。然后你可以像你正在做的那样接受用户输入 T、A、P,但你必须使用相应的比较器对数组进行排序。伪代码如下:
You can create one Class with all the three attributes Author, Title, Pages. And then you can create 3 comparators which will compare in terms of Author, Title and Pages independently. And then you can take user input T, A, P like what you are doing buy you have to sort the array with the corresponding comparators. Pseudo code would be like:
改编自 @ScottStanchfield 的这篇文章: 排序列表通过其变量之一
我不懂Java,所以如果您发现任何错误,请纠正!
Adapted from this post by @ScottStanchfield: sorting List<Class> by one of its variable
I don't know Java, so if you see any mistakes, please correct them!