我无法在我的程序中实现比较器。
该程序最初是作为如何使用swing创建菜单的示例编写的。该程序只显示菜单,子菜单和电影列表及其标题,日期和工作室。一个较大的数组以其自己的数组存储每个电影及其标题,日期和工作室。
现在,我必须找到一种使用比较器对这些字符串进行排序的方法。像这样:
Arrays.sort(data, new Comparator<String[]>() {
public int compare(final String[] entry1, final String[] entry2) {
final String field1 = entry1[0];
final String field22 = entry2[0];
return field1.compareTo(field2);
}
});
问题是,我从未使用过比较器,也不确定如何实现它。
我在下面添加了原始程序的源代码。
非常感谢任何和所有帮助。
谢谢。
编辑这个程序类似于Java教科书中的程序,但我们特别被告知不要以教科书的方式解决这种问题。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
class MenuExample extends Frame implements ActionListener
{
MenuBar mbar;
Menu menu,submenu;
MenuItem m1,m2,m3,m4,m5;
public MenuExample ()
{
String[][] data = new String[][]
{
new String[] { "Casablanca", "Warner Brothers", "1942" },
new String[] { "Citizen Kane", "RKO Pictures", "1941" },
new String[] { "Singin' in the Rain", "MGM", "1952" },
new String[] { "The Wizard of OZ", "MGM", "1930"}
};
// Set frame properties
setTitle("AWT Menu"); // Set the title
setSize(800,500); // Set size to the frame
setLayout(new FlowLayout()); // Set the layout
setVisible(true); // Make the frame visible
setLocationRelativeTo(null); // Center the frame
// Create the menu bar
mbar=new MenuBar();
// Create the menu
menu=new Menu("Menu");
// Create the submenu
submenu=new Menu("Sub Menu");
// Create MenuItems
m1=new MenuItem("Menu Item 1");
m2=new MenuItem("Menu Item 2");
m3=new MenuItem("Menu Item 3");
m4=new MenuItem("Menu Item 4");
m5=new MenuItem("Menu Item 5");
m1.addActionListener(this);
// Attach menu items to menu
menu.add(m1);
menu.add(m2);
menu.add(m3);
// Attach menu items to submenu
submenu.add(m4);
submenu.add(m5);
// Attach submenu to menu
menu.add(submenu);
// Attach menu to menu bar
mbar.add(menu);
// Set menu bar to the frame
setMenuBar(mbar);
JPanel textPanel = new JPanel();
JTextPane textArea = new JTextPane();
Dimension textAreaDimensions = new Dimension(100, 300);
textArea.setPreferredSize(textAreaDimensions);
//width: 770 height: 1000
JScrollPane scroll = new JScrollPane (textArea,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
Document doc=textArea.getDocument();
try
{
for(int i=0;i<4;i++)
{
for(int j=0;j<3;j++)
doc.insertString(doc.getLength(),data[i][j]+"\t",
textArea.getStyle("bold"));
doc.insertString(doc.getLength(),"\n",
textArea.getStyle("bold"));
}//end for
}//end try
catch (Exception e) {}
textPanel.add(scroll);
add(textPanel);
validate();
repaint();
}//end MenuExample
public static void main(String args[])
{
new MenuExample();
}//end main
public void actionPerformed(ActionEvent e)
{
System.out.println(e.getActionCommand());
}//end actionPerformed
}//end class
答案 0 :(得分:1)
我只是将这个简单的示例放在一起,以显示使用Comparator
的两种不同方式:
import java.util.Arrays;
import java.util.Comparator;
public class ArraySort
{
public static void main(String[] args)
{
String[][] data = new String[][]
{
new String[] { "Casablanca", "Warner Brothers", "1942" },
new String[] { "Citizen Kane", "RKO Pictures", "1941" },
new String[] { "Singin' in the Rain", "MGM", "1952" },
new String[] { "The Wizard of OZ", "MGM", "1930"},
new String[] { "AaaaaThe Wizard of OZ", "MGM", "1943"}
};
Arrays.sort(data, new Comparator<String[]>() {
@Override
public int compare(final String[] entry1, final String[] entry2) {
final String field1 = entry1[0];
final String field2 = entry2[0];
return field1.compareTo(field2);
}
});
print(data);
System.out.println();
Arrays.sort(data, new SortByDate());
print(data);
System.out.println();
Arrays.sort(data, new SortByCompany());
print(data);
}
public static void print(String[][] data){
for (String[] array : data){
for (String s : array){
System.out.print(s + " ");
}
System.out.println();
}
}
}
public class SortByDate implements Comparator<String[]>{
@Override
public int compare(final String[] entry1, final String[] entry2) {
final String field1 = entry1[2];
final String field2 = entry2[2];
return field1.compareTo(field2);
}
}
public class SortByCompany implements Comparator<String[]>{
@Override
public int compare(final String[] entry1, final String[] entry2) {
final String field1 = entry1[1];
final String field2 = entry2[1];
return field1.compareTo(field2);
}
}
输出:
AaaaaThe Wizard of OZ MGM 1943
Casablanca Warner Brothers 1942
Citizen Kane RKO Pictures 1941
Singin' in the Rain MGM 1952
The Wizard of OZ MGM 1930
The Wizard of OZ MGM 1930
Citizen Kane RKO Pictures 1941
Casablanca Warner Brothers 1942
AaaaaThe Wizard of OZ MGM 1943
Singin' in the Rain MGM 1952
The Wizard of OZ MGM 1930
AaaaaThe Wizard of OZ MGM 1943
Singin' in the Rain MGM 1952
Citizen Kane RKO Pictures 1941
Casablanca Warner Brothers 1942
答案 1 :(得分:1)
查看Comparator
的文档,您会注意到它是一个Java接口,这意味着您可以创建一个实现Comparator.compare(T o1, T o2)
和Comparator.equals(Object obj)
方法的类。
假设您将所有字符串属性封装在Movie
对象中(我强烈推荐),您可以像这样实现MovieNameComparator
类:
public class MovieNameComparator implements Comparator<Movie> {
@override
public int compare(Movie m1, Movie2) {
return m1.getName().compareToIgnoreCase(m2.getName());
}
@override
public boolean equals(Movie otherMovie) {
return this.getName().equals(otherMovie.getName());
}
}
现在,您可以使用此比较器按电影名称对电影数组进行排序:
Arrays.sort(data, new MovieNameComparator());
如果要按其他影片属性排序,可以按照相同的方法创建更多比较器类,例如MovieDateComparator
,MovieProductionStudioComparator
等。
更新:根据要求,Movie
课程可能如下所示:
public class Movie{
private String name;
private String studio;
private Date releaseDate;
// add getters and setters here...
}