在从“data.txt”文件中读取后,如何将代码添加到以下程序以对包含Person对象的数组arr进行排序[有关详细信息,请参阅getData()方法]。 这对我来说很难,因为我一生中从未使用过java比较器,而且更习惯使用C ++进行排序。
import java.awt.*;
import java.util.*;
import java.io.*;
import javax.swing.*;
import java.awt.event.*;
public class RectangleProgram extends JFrame
{
private JLabel Title, LastName, FirstName, LawnSize, TotalCost, RunningTotal;
private JTextField Field1, Field2, Field3, Field4, Field5;
private JButton Next, Quit;
public class Person extends RectangleProgram
{
String firstname; String lastname; int z;
public Person(String l, String m, int e)
{
firstname=l; lastname=m; z=e;
}
}
public static Person [] arr=new Person [100]; // store all the data in this array!
public void getData()
{
try{
Scanner sc=new Scanner(new File("data.txt"));
int r=sc.nextInt();
for (int g=0; g<r; g++)
{
String firstnamez, lastnamez; int rm;
firstnamez=sc.next();
lastnamez=sc.next();rm=sc.nextInt();
Person xe=new Person(firstnamez, lastnamez, rm);
arr[g]=xe;
}
}
catch(FileNotFoundException fnfe) {
System.out.println(fnfe.getMessage());
}
}
private re x= new re(); private me xx=new me();
public RectangleProgram()
{
setSize(250,200);
setTitle("Sample1");
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container pane=getContentPane();
pane.setLayout(new FlowLayout(FlowLayout.CENTER, 5,2));
Title=new JLabel("Green and Grow Mowing Company");
LastName=new JLabel("Last Name: ");
FirstName=new JLabel("First Name: ");
LawnSize=new JLabel("Lawn Size: ");
TotalCost=new JLabel("Total Cost: ");
RunningTotal=new JLabel ("Running Total: ");
Field1= new JTextField (10); Field2= new JTextField(10); Field3= new JTextField(10); Field4= new JTextField(10); Field5= new JTextField(10);
Next= new JButton("Next"); Quit= new JButton("Quit");
Next.addActionListener(x); Quit.addActionListener(xx);
pane.add(Title); pane.add(LastName); pane.add(Field1); pane.add(FirstName);
pane.add(Field2); pane.add(LawnSize); pane.add(Field3); pane.add(TotalCost); pane.add(Field4);
pane.add(RunningTotal); pane.add(Field5);
pane.add(Next); pane.add(Quit);
setVisible(true);
}
public class re implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
//Field2.setText(""+3);
getData();
}
}
public class me implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
public static void main(String[] args)
{
RectangleProgram x= new RectangleProgram();
}
}