我最近完成了一项任务,要求我们按字母顺序从文本文件中对名称进行排序,我使用了三个不同的类来使其正常工作。
class Person {
String firstName;
String lastName;
}
然后我创建了这个以按姓氏按字母顺序排序,然后按名字
排序 class SortNames {
void sortNames(Person[] arr, int type) {
if (type == 1) {
int j;
boolean flag = true; // will determine when the sort is finished
Person temp;
while (flag) {
flag = false;
for (j = 0; j < arr.length - 1; j++) {
if (arr[j].lastName.compareToIgnoreCase(arr[j + 1].lastName) > 0) { // ascending
// sort
temp = arr[j];
arr[j] = arr[j + 1]; // swapping
arr[j + 1] = temp;
flag = true;
}
}
}
for (int k = 0; k < arr.length; k++)
System.out.println(arr[k].firstName +" "+arr[k].lastName);
} else if (type == 2) {
int j;
boolean flag = true; // will determine when the sort is finished
Person temp;
while (flag) {
flag = false;
for (j = 0; j < arr.length - 1; j++) {
if (arr[j].firstName.compareToIgnoreCase(arr[j + 1].firstName) > 0) { // ascending
// sort
temp = arr[j];
arr[j] = arr[j + 1]; // swapping
arr[j + 1] = temp;
flag = true;
}
}
}
for (int k = 0; k < arr.length; k++)
System.out.println(arr[k].firstName +" "+arr[k].lastName);
}
}
}
然后我使用一个简单的程序打印所有名称,首先按照给定的顺序打印,然后按姓氏顺序打印,然后按名字顺序打印。
import java.io.*;
import java.util.*;
public class SortNameApp {
public static void main(String[] args) throws IOException {
Scanner scanner=new Scanner(new File(args[0]));
try {
int namesCount = Integer.parseInt(scanner.nextLine());
Person[] arr = new Person[namesCount];
String line = null;
int i = 0;
while (scanner.hasNextLine()) {
line = scanner.nextLine();
Person person = new Person();
person.firstName = line.split(" ")[0];
person.lastName = line.substring(person.firstName.length(),
line.length()).trim();
arr[i] = person;
System.out.println(arr[i].firstName +" "+arr[i].lastName);
i++;
}
System.out.println("---------SORT BY LAST NAME---------");
new SortNames().sortNames(arr, 1);// sort by last name
System.out.println("---------SORT BY FIRST NAME---------");
new SortNames().sortNames(arr, 2);// sort by first name
} catch (IndexOutOfBoundsException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
我想创建一个GUI,在不同的相对JtextFields中打印不同按钮完成所有三件事,但是当我打印出来时,只有一个名字打印到jtextfield上,即使文本文件有大约有30个名字。这就是我在“加载文件”按钮下调用的内容
public void Read() {
try {
String file = filename.getText();
int filesize = file.length();
Scanner input = new Scanner(getClass().getResourceAsStream(file));
int namesCount = Integer.parseInt(input.nextLine());
Person[] arr = new Person[namesCount];
String line = null;
int i = 0;
while (input.hasNextLine()) {
line = input.nextLine();
Person person = new Person();
person.firstName = line.split(" ")[0];
person.lastName = line.substring(person.firstName.length(),
line.length()).trim();
arr[i] = person;
display.setText(arr[i].firstName +" "+arr[i].lastName);
i++;
对于排序我尝试这样做,但它仍然无法正常工作:
display2.setText(new SortNames().sortNames(arr, 2));
这里
做这样的事情的正确方法是什么?
到目前为止,这是我的GUI
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import java.util.*;
public class SortNamesGUI extends JFrame {
private final LayoutManager layout;
private final LayoutManager layout2;
private JButton loadButton;
private JTextField filename;
private JTextArea display;
private JTextArea display2;
private JTextArea display3;
public SortNamesGUI()
{
super("Sorting Names");
setLayout(new BorderLayout());
JPanel buttonPanel = new JPanel();
layout = new FlowLayout();
buttonPanel.setLayout(layout);
JButton LoadFile = new JButton("Load File");
JButton FirstName = new JButton("Sort First Name");
JButton LastName = new JButton("Sort Last Name");
filename = new JTextField("Data file", 15);
buttonPanel.add(filename);
buttonPanel.add(LoadFile);
buttonPanel.add(FirstName);
buttonPanel.add(LastName);
JPanel DisplayPanel = new JPanel();
layout2 = new GridLayout(1,3);
DisplayPanel.setLayout(layout2);
display = new JTextArea("Unsorted list");
display2 = new JTextArea("Sorted based on first name");
display3 = new JTextArea("Sorted based on last name");
DisplayPanel.add(display);
DisplayPanel.add(display2);
DisplayPanel.add(display3);
DisplayPanel.add(new JScrollPane(display));
DisplayPanel.add(new JScrollPane(display2));
DisplayPanel.add(new JScrollPane(display3));
add(buttonPanel,BorderLayout.NORTH);
add(DisplayPanel,BorderLayout.CENTER);
LoadFile.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
Read(); }
}
);
}
/*public void actionPerformed(ActionEvent event) {
Read();
}*/
public void Read() {
try {
String file = filename.getText();
int filesize = file.length();
Scanner input = new Scanner(getClass().getResourceAsStream(file));
int namesCount = Integer.parseInt(input.nextLine());
Person[] arr = new Person[namesCount];
String line = null;
int i = 0;
while (input.hasNextLine()) {
line = input.nextLine();
Person person = new Person();
person.firstName = line.split(" ")[0];
person.lastName = line.substring(person.firstName.length(),
line.length()).trim();
arr[i] = person;
display.setText(arr[i].firstName +" "+arr[i].lastName);
i++;
person.toString();
}
} catch (IndexOutOfBoundsException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void SoftFirstName() {
String file = filename.getText();
int filesize = file.length();
Scanner input = new Scanner(getClass().getResourceAsStream(file));
int namesCount = Integer.parseInt(input.nextLine());
Person[] arr = new Person[namesCount];
new SortNames().sortNames(arr, 2);// sort by first name
display2.setText(new SortNames().sortNames(arr, 2));
}
public static void main(String[] args) {
SortNamesGUI testing= new SortNamesGUI();
testing.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
testing.setSize(620, 180);
testing.setVisible(true);
}
}
答案 0 :(得分:0)
if(isset($this->_cart_contents[$rowid]))
{
$this->_cart_contents[$rowid]['qty']++;
return $rowid;
}
unset($this->_cart_contents[$rowid]);
如果您尝试过此操作,则很可能会遇到编译器错误。这是因为display2.setText(new SortNames().sortNames(arr, 2));
方法返回sortNames()
,但void
方法需要setText()
参数。您需要修改String
以返回已排序的数组,而不是直接将其打印出来。在原始代码中,sortNames()
应获取已排序的数组,然后将其打印出来。这是我们称之为单一责任原则的一个例子。每种方法都应该负责一项任务。在这种情况下,您的main()
会做两件事:1。对数组进行排序,然后2.打印已排序的数组。如果将这两个任务分成不同的方法,那么当您将其转换为GUI时,它将使您的工作变得更加容易。
将这两项任务分开后,您需要决定如何在JTextAray中显示名称。我能想到至少两个解决方案:
将整个数组转换为String并在JTextArea中显示结果。这应该很容易实现,但输出将有一个由方括号内的逗号分隔的名称列表。
将每个名称一次添加到JTextArea。请注意,sortNames()
将完全用给定文本替换JTextArea的全部内容。我建议您查看the documentation for JTextArea以找到对您的目的有用的其他方法。