在 Java 中使用比较器进行数组排序
Array Sorting Using A Comparator in Java
我在我的程序中实现比较器时遇到问题。
该程序最初是作为如何使用 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
我只是把这个简单的例子放在一起来展示使用 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
查看 Comparator
的文档,您会注意到它是一个 Java 接口,这意味着您可以创建一个 class 来实现 Comparator.compare(T o1, T o2)
和Comparator.equals(Object obj)
方法。
所以假设您将所有字符串属性封装在一个 Movie
对象中(我强烈推荐),您可以像这样实现一个 MovieNameComparator
class:
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());
如果你想按其他电影属性排序,你可以按照相同的方法创建更多的比较器class,如MovieDateComparator
、MovieProductionStudioComparator
等
更新:根据要求,Movie
class 可能类似于:
public class Movie{
private String name;
private String studio;
private Date releaseDate;
// add getters and setters here...
}
我在我的程序中实现比较器时遇到问题。
该程序最初是作为如何使用 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
我只是把这个简单的例子放在一起来展示使用 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
查看 Comparator
的文档,您会注意到它是一个 Java 接口,这意味着您可以创建一个 class 来实现 Comparator.compare(T o1, T o2)
和Comparator.equals(Object obj)
方法。
所以假设您将所有字符串属性封装在一个 Movie
对象中(我强烈推荐),您可以像这样实现一个 MovieNameComparator
class:
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());
如果你想按其他电影属性排序,你可以按照相同的方法创建更多的比较器class,如MovieDateComparator
、MovieProductionStudioComparator
等
更新:根据要求,Movie
class 可能类似于:
public class Movie{
private String name;
private String studio;
private Date releaseDate;
// add getters and setters here...
}